1

这一定是一个愚蠢的问题,因为在 stackoverflow 中有很多相关的问题,也许答案很简单。但无论如何,它就在这里。我有一个按钮并为它创建一个可绘制的 xml。所以在我的按钮下面是代码:

<Button
            android:id="@+id/button2"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="match_parent"
            android:layout_height="30dip"           
            android:background="@drawable/button2" 
            />

现在在我的drawable/button2下我创建了一个我没有使用图像的选择器。这是我的代码:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" >
    <shape>
        <solid
            android:color="#FF9E35" />
        <stroke
            android:width="1dp"
            android:color="#992f2f" />
        <corners
            android:radius="0dp"
            android:topLeftRadius="8dp"
            android:bottomLeftRadius="0dp"
            android:topRightRadius="0dp"
            android:bottomRightRadius="8dp" />
        <padding
            android:left="5dp"
            android:top="5dp"
            android:right="5dp"
            android:bottom="5dp" />
    </shape>
 </item>
 <item>
    <shape>
        <gradient
            android:startColor="#FF9E35"
            android:endColor="#992f2f"
            android:angle="270" />
        <stroke
            android:width="1dp"
            android:color="#992f2f" />
        <corners
            android:radius="0dp"
            android:topLeftRadius="8dp"
            android:bottomLeftRadius="0dp"
            android:topRightRadius="0dp"
            android:bottomRightRadius="8dp" />
        <padding
            android:left="5dp"
            android:top="5dp"
            android:right="5dp"
            android:bottom="5dp" />
    </shape>
    </item>
   </selector>

现在我要完成的是,当我单击该按钮时,它将其背景颜色更改为 state_pressed 所暗示的内容,并在单击另一个按钮时恢复正常(很可能是切换按钮)无论如何在代码上做在xml选择器后面还是在里面?

谢谢,

JC

4

1 回答 1

2

您实际上可以在 Java 代码中执行的操作是:

public class LaunchActivity extends Activity implements OnTouchListener{

    private Button yourButton;


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);        
        setContentView(R.layout.main);      

        yourButton= (Button)findViewById(R.id.yourButton);
        yourButton.setOnTouchListener(this); 

    }

    @Override
    public boolean onTouch(final View view, MotionEvent event) {

    final int action = event.getAction();

        if(view.getId()==R.id.yourButton){
            if(action == MotionEvent.ACTION_DOWN)
                  yourButton.setBackgroundResource(R.drawable.ic_button_pressed);
            if(action == MotionEvent.ACTION_UP)
                  yourButton.setBackgroundResource(R.drawable.ic_button_normal);
        }

 }
}

如果他被按下,这将改变按钮的背景。当用户松开手指时,它会再次改变背景。如果您只想在按下另一个按钮时更改背景,请不要编写 ACTION_UP 情况,而在另一个按钮的另一个 ACTION_DOWN 情况下,更改其他按钮的背景,请参见下文:

 @Override
        public boolean onTouch(final View view, MotionEvent event) {

    final int action = event.getAction();

        if(view.getId()==R.id.yourButton){
            if(action == MotionEvent.ACTION_DOWN){
                  secondButton.setBackgroundResource(R.drawable.ic_button_normal);
                  yourButton.setBackgroundResource(R.drawable.ic_button_pressed);
            }
        }
            if(view.getId()==R.id.secondButton){
            if(action == MotionEvent.ACTION_DOWN){
                  yourButton.setBackgroundResource(R.drawable.ic_button_normal);
                  secondButton.setBackgroundResource(R.drawable.ic_button_pressed);
            }
        }

 }

希望能帮助到你 !

于 2012-08-13T08:42:00.460 回答