網頁

2017年2月17日 星期五

[ Android Studio ] Button 按鈕 - 按下與彈出圖案是不同的效果

按鈕「收入」被按下時改變背景圖案,放開時又變回原來的圖案的方法

一、setOnTouchListener
介紹:當一個觸摸事件被調度到這一觀點被調用回調接口定義。

View:觸摸事件。
MotionEvent:有關該事件的完整信息對象。
https://developer.android.com/reference/android/view/MotionEvent.html

二、語法:

b2=(Button)findViewById(R.id.button2);
b2.setOnTouchListener(new View.OnTouchListener() {
    @Override    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                b2.setBackgroundResource(R.drawable.b4);
                break;
            case MotionEvent.ACTION_UP:
                b2.setBackgroundResource(R.drawable.b1);
                break;

        }

        return false;
    }
});

沒有留言:

張貼留言

影片的問題請留在影片的留言區裡。
部落格不會另外通知給我,所以很難發現你有留言。