2014-12-19 53 views
-2

我想這樣做,當我按住按鈕的alpha值減少。 當我釋放它時,它將alpha值設置回1並執行操作。 此按鈕已編程。如何以編程方式更改Android按鈕的Alpha值?

你會如何檢查發佈是否在按鈕上? 事情是這樣的: btn.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View view, MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN) { btn.setAlpha(0.7f); } else if (event.getAction() == MotionEvent.ACTION_UP) { btn.setAlpha(0.85f); if (btn.isFocused()) { // Do something here } } return true; }); }

+0

你應該嘗試使用'onTouch'事件,然後找到它是'MotionEvent.ACTION_UP'或'MotionEvent.ACTION_DOWN'並執行你的操作 – Panther 2014-12-19 05:36:04

回答

1

嘗試使用按鈕的觸摸事件對其執行您的操作類似 東西下面

btn.setOnTouchListener(新OnTouchListener(){

@Override 
    public boolean onTouch(View arg0, MotionEvent arg1) { 
     if (arg1.getAction()==MotionEvent.ACTION_DOWN) { 
     // your down operations 
     //in your case btn.setAlpha() here 
     } 
     else if (arg1.getAction()==MotionEvent.ACTION_UP){ 
     // your up operations 
     //in your case btn.setAlpha() here 

     } 
    } 
} 
+0

你還會如何檢查發佈時用戶是否還在按鈕上? 與代碼編輯原始帖子。 – nadnav 2014-12-19 05:52:05

+0

檢查觸摸位置和按鈕視圖的邊界。如果位置在裏面,那麼你知道它的內部:P – Panther 2014-12-19 06:00:03

相關問題