14

レーティングスターのように、星型のボタンを1つアプリに配置したいと思います。私はすでに1つ星の評価バーを使おうとしましたが、残念ながらボタンとしては機能しません。

ボタンとして機能させたいのですが、選択した状態の背景が黄色の場合はさらに良いです...何か提案はありますか?ありがとう。

4

9 に答える 9

28

確かに、次のようなレイアウトリソースを使用してください。

<ImageButton android:id="@+id/favorite"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:src="@drawable/star"
        android:background="#00ffffff"
        android:onClick="onToggleStar"/>

これを、次の場所にXMLファイルとして保存されるこのように定義された状態リストドローアブルと組み合わせることができますres/drawable/star.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
          android:drawable="@drawable/button_pressed" /> <!-- pressed -->
    <item android:state_focused="true"
          android:drawable="@drawable/button_focused" /> <!-- focused -->
    <item android:state_hovered="true"
          android:drawable="@drawable/button_focused" /> <!-- hovered -->
    <item android:drawable="@drawable/button_normal" /> <!-- default -->
</selector>

Android自体に同梱されている画像は次のとおりです。

  • オフ オフ
  • の上 の上
  • 無効 無効
  • 押された 押された
  • オフプレス オフプレス
于 2011-11-23T15:01:49.317 に答える
13

btn_star組み込みのステートリストドローアブルを使用できます。

<ImageButton
    android:id="@+id/favorite_button"
    android:layout_width="60dp"
    android:layout_height="60dp"
    android:src="@android:drawable/btn_star"  //This one! 
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:background="#00ffffff" /> //Needed to remove the button background
于 2012-07-07T16:55:36.773 に答える
3

お気に入りのボタンをチェックボックスのように動作させたいので、onClickListenerに画像切り替えロジックを追加しなくても、これでうまくいきました。私のJavaコードではmyFavoriteToggleButton.isChecked()、実行したアクションを判別するために使用できます。

    <CheckBox
    android:button="@android:drawable/btn_star"
    android:padding="@dimen/activity_horizontal_margin"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/favorite"

    android:background="@android:color/transparent"
    android:layout_gravity="center"/>
于 2016-11-25T06:32:41.087 に答える
2

これは、最初から最後まで、カスタムボタンを作成するための非常に優れたチュートリアルです。本当に巧妙になりたい場合は、ボタンの状態ごとに独自の星の画像を作成することもできます。

http://www.youtube.com/watch?v=zXXCFmfJMNw

于 2011-11-23T15:02:24.507 に答える
2

アクティビティに以下のコードを追加するだけです。

final ImageButton ButtonStar = (ImageButton) findViewById(R.id.star);
    ButtonStar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (isEnable){
                ButtonStar.setImageDrawable(ContextCompat.getDrawable(getApplicationContext(),android.R.drawable.btn_star_big_off));
            }else{
                ButtonStar.setImageDrawable(ContextCompat.getDrawable(getApplicationContext(),android.R.drawable.btn_star_big_on));
            }
            isEnable = !isEnable;
        }
    });

注意:ブールisEnable=falseグローバルを定義します。これは私のImageButtonxmlコードです:

<ImageButton
                android:id="@+id/star"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@null"
                android:src="@android:drawable/btn_star" />
于 2017-08-08T08:19:15.620 に答える
1

Androidにはそのような組み込みのコントロールがないのではないかと思います。Button(またはImageButton)を使用し、背景(または画像リソース)を状態のあるドローアブルに設定する必要があります。

于 2011-11-23T14:59:58.993 に答える
1

どうStateListDrawableですかButton?それを試してみてください、バディ:)

http://developer.android.com/reference/android/graphics/drawable/StateListDrawable.html

于 2011-11-23T14:59:09.083 に答える
1

テキストビューとして挿入してみることができます。これを行うのに私が見つけた最も簡単な方法でした。

レイアウトxmlで、テキストビューとして追加しました

<TextView
    android:id="@+id/tv_star_fav"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#00ffffff"
    android:onClick="onToggleStar"
    android:text="★"
    android:textSize="40sp" />

次に、コードに次のような関数を記述しました。

boolean isfavourite;


public void onToggleStar(View view){
    TextView favouriteStar = (TextView) view;
    if(!isfavourite){
        // if the star is not already selected and you select it
        isfavourite = true;
        favouriteStar.setTextColor(Color.parseColor("#FFD600"));
    }else{
        // if the star is already selected and you unselect it
        isfavourite = false;
        favouriteStar.setTextColor(Color.parseColor("#9E9E9E"));
    }

}
于 2017-12-05T19:14:39.153 に答える
0

(または)selectorの状態に役立つaを使用する必要があるかもしれません。これを参照してくださいButtonImageButton

于 2011-11-23T15:05:12.187 に答える