12

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

        <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" />

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

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

4 回答 4

15

如果选中或未选中,您应该使用切换按钮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>

然后像这样在你的按钮中定义它

  android:background="@drawable/like_button"

编辑

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

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

于 2013-01-14T19:38:04.993 回答
6

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

<?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

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

于 2013-01-14T19:48:28.733 回答
3

要更改背景图像:

public void onClick(View v) {
   if(v == ButtonName) {
     ButtonName.setImageResource(R.drawable.ImageName);
   }
}

或者,使用 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/login_selected" /> <!-- pressed -->
  <item android:state_focused="true"
   android:drawable="@drawable/login_mouse_over" /> <!-- focused -->
  <item android:drawable="@drawable/login" /> <!-- default -->
</selector>

在 OnClick 中,只需添加以下代码:

ButtonName.setBackgroundDrawable(getResources().getDrawable(R.drawable.ImageName));
于 2013-08-09T22:20:01.333 回答
0

是的,您可以Drawable根据View呈现它的状态选择一个。

这是一种Selector可绘制对象的确切目的。在此处查看示例和指南:http: //developer.android.com/guide/topics/resources/drawable-resource.html#StateList

基本上,每个item选择器都定义了哪些状态具有哪些值。它还定义了哪个可绘制对象代表这组值。

接下来,您可以View从代码中设置 a 的状态,例如。

celsiusButton.setPressed(true);

这在实践中非常有用,因为您将 UI 设置与模型/控制器分开。当代码不负责以直接方式更改应用程序的 UI 时,维护大量可绘制对象会变得更容易。

我的工作选择器的一个例子是:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/ic_background_pressed" android:state_pressed="true"/>
    <item android:drawable="@drawable/ic_background_focused" android:state_focused="true"/>
    <item android:drawable="@drawable/ic_background_default" />

</selector>

此示例根据其状态呈现按钮背景。

于 2013-01-14T19:42:42.137 回答