如何在XML文件中设置选中/未选中按钮的背景 [英] How to set background of selected/unselected Button in the XML file

查看:285
本文介绍了如何在XML文件中设置选中/未选中按钮的背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些自定义按钮,这些按钮应该具有不同的背景,具体取决于它们是否被选中。我想知道是否有一种方法可以在XML文件中说明这一点。我有一个摄氏温度和华氏温度的按钮。我希望它可以在以下位置工作:如果选择了一个,它将保持按下状态并且无法单击,而可以按下另一个按钮。

I have custom buttons that are supposed to have different backgrounds depending if they are selected or not selected. I want to know if there is a way to state this in the XML file. I have a button for Celsius and a button for Fahrenheit. I want it to work where if one is selected, it stays "pressed" and unable to be clicked, while the other button can be pressed.

        <Button
            android:id="@+id/celsiusButton"
            android:text="C"
            android:background="@drawable/button_unpressed_shape"
            android:layout_weight="3"
            android:layout_height="match_parent"
            android:layout_width="0dip"
            android:gravity="center" />

        <Button
            android:id="@+id/fahrenheitButton"
            android:text="F"
            android:background="@drawable/button_unpressed_shape"
            android:layout_weight="3"
            android:layout_height="match_parent"
            android:layout_width="0dip"
            android:gravity="center" />

摄氏按钮默认为选中状态。我尝试在我的代码中像这样处理它,但似乎有点混乱:

The Celsius button is defaulted to selected. I try working on it like this in my code, but it just seems to messy:

    tempText = (TextView) findViewById( R.id.temperatureId );
    celsiusButton = (Button) findViewById( R.id.celsiusButton );
    celsiusButton.setBackgroundDrawable( getResources().getDrawable( R.drawable.button_pressed_shape ) );
    celsiusButton.setClickable( false );

    celsiusButton.setOnClickListener( new OnClickListener() {
        public void onClick(View v) {
            if( hasRead ) {
                    celsiusButton.setBackgroundDrawable( getResources().getDrawable( R.drawable.button_pressed_shape ) );
                    celsiusButton.setClickable( false );
                    fahrenheitButton.setBackgroundDrawable( getResources().getDrawable( R.drawable.button_unpressed_shape ) );
                    fahrenheitButton.setClickable( true );
                    temperature = ( ( ( temperature - 32 ) * 5 ) / 9 );
                    tempText.setText( Double.toString( temperature ).substring( 0, ( Double.toString( temperature ).length() - 2 ) ) + " C" );
            }
        }       
    });

    fahrenheitButton = (Button) findViewById( R.id.fahrenheitButton );
    fahrenheitButton.setOnClickListener( new OnClickListener() {
        public void onClick( View v ) {
            if( hasRead ) {
                fahrenheitButton.setBackgroundDrawable( getResources().getDrawable( R.drawable.button_pressed_shape ) );
                celsiusButton.setBackgroundDrawable( getResources().getDrawable( R.drawable.button_unpressed_shape ) );
                celsiusButton.setClickable( true );
                fahrenheitButton.setClickable( false );
                temperature = ( ( temperature * 9 ) / 5 ) + 32;
                tempText.setText( Double.toString( temperature ).substring( 0, ( Double.toString( temperature ).length() - 2 ) ) + "° F" );
            }
        }
    });


推荐答案

如果选中或未选中,则应使用切换开关按钮 https://developer.android.com/reference/android/widget/ToggleButton.html

If it's selected or not selected you should use a toggle button https://developer.android.com/reference/android/widget/ToggleButton.html

请注意,该状态仍然有4种状态

Be aware that there are still 4 states for that

您可以在这样的选择器中定义它们

You define them in a selector like this

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:state_pressed="true" android:drawable="@drawable/likeactivepressed" />
    <item android:state_pressed="true" android:drawable="@drawable/likeinitialpressed"/>
    <item android:state_checked="true" android:drawable="@drawable/likeon"/>
    <item android:drawable="@drawable/likeinitial"/>
</selector>

然后在您的按钮中定义它,如下所示:

Then define it in your button like this

  android:background="@drawable/like_button"

编辑

您实际上可以只使用1个按钮即可使用。另外,您也可以使用2个单选按钮

You could actually just use 1 button for your use. Alternatively you can use 2 radio buttons

https: //developer.android.com/reference/android/widget/RadioButton.html

这篇关于如何在XML文件中设置选中/未选中按钮的背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆