14

我想在我的应用程序中放置一个星形按钮,就像评级星一样。我已经尝试过使用一星评级栏,但不幸的是它不能用作按钮。

我希望它作为一个按钮工作,如果所选状态的背景是黄色的,那就更好了......有什么建议吗?谢谢。

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_starstatelist drawable。

<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;
        }
    });

注意:define boolean isEnable=false global。这是我的 ImageButton xml 代码:

<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

怎么StateListDrawableButton?试试吧,伙计:)

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

于 2011-11-23T14:59:09.083 回答
1

恐怕Android没有这样的内置控件。您将需要使用 a Button(或 an ImageButton),并将背景(或图像资源)设置为带有 states 的可绘制对象

于 2011-11-23T14:59:58.993 回答
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

可能您需要使用对 a (或 an )selector的状态有帮助的 a 。参考这个ButtonImageButton

于 2011-11-23T15:05:12.187 回答