怎么设置android的relativelayout按下时候的事件
为Button绑定 OnTouchListener 监听器。
public class AppMain extends Activity{
private Button mButton;
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ButtonListener b = new ButtonListener();
mButton = (Button)findViewById(R.id.button1);
mButton.setOnClickListener(b);
mButton.setOnTouchListener(b);
mButton.setBackgroundResource(R.drawable.green);
}
class ButtonListener implements OnClickListener, OnTouchListener{
public void onClick(View v) {
if(v.getId() == R.id.button1){
Log.d("test", "cansal button ---> click");
}
}
public boolean onTouch(View v, MotionEvent event) {
if(v.getId() == R.id.button1){
if(event.getAction() == MotionEvent.ACTION_UP){
Log.d("test", "cansal button ---> cancel");
mButton.setBackgroundResource(R.drawable.green);
}
if(event.getAction() == MotionEvent.ACTION_DOWN){
Log.d("test", "cansal button ---> down");
mButton.setBackgroundResource(R.drawable.yellow);
}
}
return false;
}
}
}
地址:http://www.linuxidc.com/Linux/2011-10/45976.htm