如何在XML文件中设置所选/未选定Button的背景

Jui*_*iCe 12 xml android button

我有自定义按钮应该具有不同的背景,具体取决于它们是否被选中.我想知道是否有办法在XML文件中说明这一点.我有一个Celsius按钮和一个华氏按钮.我希望它能够工作,如果选择了一个,它会保持"按下"而无法点击,而另一个按钮可以被按下.

        <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" />
Run Code Online (Sandbox Code Playgroud)

摄氏度按钮默认为选中状态.我在我的代码中尝试这样做,但它似乎很乱:

    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" );
            }
        }
    });
Run Code Online (Sandbox Code Playgroud)

jid*_*vah 13

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

请注意,仍有4种状态

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

<?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>
Run Code Online (Sandbox Code Playgroud)

然后在你的按钮中定义它

  android:background="@drawable/like_button"
Run Code Online (Sandbox Code Playgroud)

编辑

实际上你可以使用1个按钮供你使用.或者,您可以使用2个单选按钮

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


Ram*_*ili 5

这用于更改按下或聚焦按钮的颜色,将此代码写入可绘制文件夹

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Button Focused-->
    <item   android:state_focused="true"
            android:state_pressed="false"
            android:drawable="@drawable/login_hover"
            />
<!-- Button Focused Pressed-->
    <item   android:state_focused="true"
            android:state_pressed="true"
            android:drawable="@drawable/login_hover"
            />
<!-- Button Pressed-->
    <item   android:state_focused="false"
            android:state_pressed="true"
            android:drawable="@drawable/login_hover"
            />
<!-- Button Default Image-->
    <item   android:drawable="@drawable/login_bg"/>

</selector
Run Code Online (Sandbox Code Playgroud)

http://nishantvnair.wordpress.com/2010/10/05/change-color-of-button-on-click-android/