- /**
- * 觸摸事件
- */
- OnTouchListener onTouchListener = new OnTouchListener()
- {
- public boolean onTouch(View v, MotionEvent event) {
- switch (event.getAction()) {
- case MotionEvent.ACTION_DOWN:
- v.setBackgroundDrawable(Util.getNinePatchDrawable(R.drawable.button_pressed));
- break;
- case MotionEvent.ACTION_UP:
- v.setBackgroundDrawable(Util.getNinePatchDrawable(R.drawable.button_normal));
- break;
- case MotionEvent.ACTION_MOVE:
- break;
- default:
- v.setBackgroundDrawable(Util.getNinePatchDrawable(R.drawable.button_normal));
- break;
- }
- return false;
- }
- };
當然還有另外一種方式,就是放在xml文件中實現
- <?xml version="1.0" encoding="utf-8"?>
- <selector xmlns:android="http://schemas.android.com/apk/res/android">
- <item android:drawable="@drawable/button_pressed" android:state_selected="true"/>
- <item android:drawable="@drawable/button_pressed" android:state_focused="true"/>
- <item android:drawable="@drawable/button_pressed" android:state_pressed="true"/>
- <item android:drawable="@drawable/button_normal"/>
- </selector>
該文件存放於drawable目錄下
使用時這樣使用:
- <Button
- android:id="@+id/button1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Button"
- android:background="@drawable/buttonstyle"
- />
這樣似乎更簡潔
更多Android相關信息見Android 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=11