【问题标题】:Android selector with image and text带有图像和文本的 Android 选择器
【发布时间】:2013-03-19 09:42:22
【问题描述】:

我有一个按钮,我给它一个颜色、图像和文本,如下所示:

android:background="@color/green"
android:drawableLeft="@drawable/custom_routes_start_button_icon"
android:text="@string/custom_route_start"

这是未选中的状态,希望选中的状态是这样的:

android:background="@color/red"
android:drawableLeft="@drawable/custom_routes_stop_button_icon"
android:text="@string/custom_route_stop"

据我所知,不可能为选择器中的项目提供文本或 drawableLeft(仅可绘制)。有人知道实现这一目标的好方法吗?也许另一个选择器也可以引用的 xml 文件?

【问题讨论】:

  • 尝试制作一个已有文本的按钮
  • 背景和 drawableLeft 应该能够使用常规选择器(对于每个)。文本可以通过使用 onTouchListener 完成(向下更改文本并重新更改)
  • “选中”是什么意思?你的意思是按钮被点击了?或者它已经成为焦点?
  • 选中状态。所以当用户点击它时

标签: java android button imagebutton android-xml


【解决方案1】:

我不确定 ToggleButton(甚至复选框)是否适合您的用例。您确实可以为您的可绘制对象使用选择器(左、右、上、下),使用带选择器的背景,甚至为您的两种状态使用不同的文本。

实际上,您可以为您的背景定义一个选择器 (/res/color/custom_color.xml)

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/green"
          android:state_checked="true" />
    <item android:color="@color/red"
        android:state_checked="false"/>
 </selector>

还有一个用于您的 drawableLeft(/res/drawable/custom_drawable.xml)。

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/custom_routes_start_button_icon"
          android:state_checked="true" />
    <item android:drawable="@color/custom_routes_stop_button_icon"
        android:state_checked="false"/>
 </selector>

最后你的 ToggleButton 定义为

android:background="@color/custom_color"
android:drawableLeft="@drawable/custom_drawable"
android:textOn="@string/custom_route_start"
android:textOff="@string/custom_route_stop"

您可能必须使用样式才能使其显示为您想要的。

虽然这是一个迟到的答案,但希望有人觉得它有用。

【讨论】:

  • 这是最好的选择。切换按钮解决了我的问题
【解决方案2】:

您应该使用两个按钮,并且只显示其中一个。在 XML 中使用 android:visibilitysetVisibility() 来显示/隐藏按钮。

所以,首先让开始按钮可见并隐藏停止按钮。当用户按下开始按钮时,隐藏开始按钮并显示停止按钮。当用户按下停止按钮时,将其隐藏并再次显示开始按钮。

【讨论】:

  • 好的,谢谢您的回答。这很容易理解,但从没想过这是解决此类问题的最佳方法。
  • 它是 :-) StateSelector 可绘制对象仅用于显示一个按钮的不同状态,例如按下,不活动等,但你想要的显然是两个按钮。在我的一个应用程序中,我有你想要的——一个交替显示的开始和停止按钮。所以我知道我在说什么;-)
【解决方案3】:

您可以通过代码更改:

在 xml 文件中写入以下代码:

 android:background="@color/green"
 android:drawableLeft="@drawable/custom_routes_start_button_icon"
 android:text="@string/custom_route_start" 

和按钮点击事件:

    yourButton = (TextView) findViewById(R.id.yourButton);

    yourButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Drawable checkImg = getApplicationContext().getResources().getDrawable(
                       R.drawable.custom_routes_stop_button_icon);
            yourButton.setCompoundDrawablesWithIntrinsicBounds(checkImg, null, null,
            null);
            yourButton.setBackgroundColor(red);
            yourButton.setText(custom_route_stop);
        }
    });

你也可以把这段代码放在Touchlistener上:

 yourButton.setOnTouchListener(new OnTouchListener() {
        boolean isTouch = false;

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                Drawable checkImg = getApplicationContext().getResources().getDrawable(
                       R.drawable.custom_routes_stop_button_icon);
                yourButton.setCompoundDrawablesWithIntrinsicBounds(checkImg, null, null, null);
                yourButton.setBackgroundColor(red);
                yourButton.setText(custom_route_stop);  
            }
            else {
                Drawable checkImg = getApplicationContext().getResources().getDrawable(
                       R.drawable.custom_routes_start_button_icon);
                yourButton.setCompoundDrawablesWithIntrinsicBounds(checkImg, null, null, null);
                yourButton.setBackgroundColor(green);
                yourButton.setText(custom_route_start);
            }
            return false;
        }
    });

【讨论】:

  • 那么在编程方式上哪种解决方案更好。更改代码中的可绘制对象、颜色和文本(如您的解决方案),或者更改两个按钮,一次只能看到 1 个?
  • 我已经实现了一个我用编程编写的,但正如你所写的,你也可以使用 2 个按钮并为两者使用相同的侦听器
【解决方案4】:

你的意思是如果你点击按钮,你想改变按钮的背景颜色吗? 是的,你可以做到的

先定义状态

private int btnState = 1;
private final static int BUTTON_STATE_SELECTED = 0;
private final static int BUTTON_STATE_UNSELECTED = 1;

然后将 id 设置为您的按钮

android:id="@+id/btnRoute"
android:background="@color/green"
android:drawableLeft="@drawable/custom_routes_start_button_icon"
android:text="@string/custom_route_start"

在您的活动中声明按钮

Button btnRoute = (Button) findviewbyid(R.id.btnRoute);

之后创建一个 onclick 监听器,它会根据状态改变按钮颜色

private View.OnClickListener mOnClickBtnRoute = new View.OnClickListener() {
switch(btnState) {
case BUTTON_STATE_SELECTED:
btnRoute.setBackgroundColor(green);
btnRoute.setText(start);
Drawable img = getContext().getResources().getDrawable( R.drawable.custom_routes_start_button_icon );
btnRoute.setCompoundDrawablesWithIntrinsicBounds( img, null, null, null );
btnState = BUTTON_STATE_UNSELECTED;
break;
case BUTTON_STATE_UNSELECTED:
btnRoute.setBackgroundColor(red);
btnRoute.setText(stop);
Drawable img = getContext().getResources().getDrawable( R.drawable.custom_routes_stop_button_icon );
btnRoute.setCompoundDrawablesWithIntrinsicBounds( img, null, null, null );
btnState = BUTTON_STATE_SELECTED;
break;
}
};

那么别忘了给按钮设置监听器

btnRoute.setOnClickListener(mOnClickBtnRoute);

请记住所有代码都在这里编码,所以可能会出现输入错误,所以请不要只是复制粘贴,而是尝试理解这个概念:) 如果您对我的回答有任何疑问,请随时在评论!

【讨论】:

  • 但是我也想改变选中状态下的图片。所以未选中的是:green、start_image、start_text 选中的是:red、stop_image、stop_text
  • 是的,你可以!使用 setcompounddrawable 函数请看我编辑的答案!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-14
  • 1970-01-01
  • 1970-01-01
  • 2012-09-03
  • 2011-10-11
相关资源
最近更新 更多