代碼實現的功能是使得一個按鈕在按下的實現顯示光色圖安,抬起的時候的顯示黃色圖案。
然後圖案自己設置形狀。當然還可以缺一角什麼的。
為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;
- }
-
- }
- }