ArrayList;import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;public class MainActivity extends Activity implements OnTouchListener {
ArrayList<Button> ButtonList = null;//Button 集合 @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButtonList = new ArrayList<Button>();
ButtonList.add((Button) findViewById(R.id.button1));
ButtonList.add((Button) findViewById(R.id.button2));
ButtonList.add((Button) findViewById(R.id.button3));
for (Button b : ButtonList)
b.setOnTouchListener(this);
} @Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
for (Button b : ButtonList)
if (b.equals(v)) {
b.setBackgroundColor(Color.RED);
} else {
b.setBackgroundColor(Color.WHITE);
}
}
return false;
}}

解决方案 »

  1.   

    XML保存在res/drawable/button.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/button_pressed" /> <!-- pressed -->
        <item android:state_focused="true"
              android:drawable="@drawable/button_focused" /> <!-- focused -->
        <item android:state_hovered="true"
              android:drawable="@drawable/button_focused" /> <!-- hovered -->
        <item android:drawable="@drawable/button_normal" /> <!-- default -->
    </selector>
    在布局文件中:<Button
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:background="@drawable/button" />
      

  2.   

    2楼正解,一般就是写个xml文件配置到控件中就能实现了
      

  3.   

    2楼正解,配置一个xml定制化,,,
      

  4.   

    按下的时候在监听器重新setBackgroundImage不就行了么。。那个按下变化的是因为背景用了selector,会根据焦点 按下神马的有不同的背景图。
      

  5.   

    我就是用这种方法的,松开还是回到以前那样,达不到要的效果 
    我只是用此举个例子,你可以加其它状态的呀,学习要能举一反三懂否
    我以前用的就是这种方法,达不到效果,所以来这里问,不明白我的意思?
    用这种方法就能实现你的意思
    不行啊,,,还是实现不了我就是用这种方法的,松开还是回到以前那样,达不到要的效果 
    我只是用此举个例子,你可以加其它状态的呀,学习要能举一反三懂否
    我以前用的就是这种方法,达不到效果,所以来这里问,不明白我的意思?
    用这种方法就能实现你的意思
    不行啊,,,还是实现不了
    楼主你看这样能实现你的要求不:
    你用RadioButton代替Button
    你知道RadioButton之间是互斥的,当同一组中的某一个RadioButton为选中状态时其它的为非选中状态,也就是说你可以分别是设置
    android:state_checked=["true" | "false"]情况下的图片资源。
    RadioButton目前的用法其实是很灵活的,他甚至可以结合Fragment来实现TabHost的效果
      

  6.   

    我就是用这种方法的,松开还是回到以前那样,达不到要的效果 
    我只是用此举个例子,你可以加其它状态的呀,学习要能举一反三懂否
    我以前用的就是这种方法,达不到效果,所以来这里问,不明白我的意思?
    用这种方法就能实现你的意思
    不行啊,,,还是实现不了我就是用这种方法的,松开还是回到以前那样,达不到要的效果 
    我只是用此举个例子,你可以加其它状态的呀,学习要能举一反三懂否
    我以前用的就是这种方法,达不到效果,所以来这里问,不明白我的意思?
    用这种方法就能实现你的意思
    不行啊,,,还是实现不了
    楼主你看这样能实现你的要求不:
    你用RadioButton代替Button
    你知道RadioButton之间是互斥的,当同一组中的某一个RadioButton为选中状态时其它的为非选中状态,也就是说你可以分别是设置
    android:state_checked=["true" | "false"]情况下的图片资源。
    RadioButton目前的用法其实是很灵活的,他甚至可以结合Fragment来实现TabHost的效果

    怎么样可以实现吗?
      

  7.   

    也就是说你要的是选中效果,不是按下效果么 
    focused 做成跟pressed一个效果,或者只要 focused跟default可以了要不你就onclick里面自己设背景,爱咋整咋整
      

  8.   

    到底有没有人会啊?selector无效果怎么回事???