ON_CANCEL从未被调用过

时间:2013-08-21 19:17:35

标签: android button ontouchlistener

所以我试图让我的按钮改变它的图像,当我按下它时它正确地改变,当我按下按钮时ACTION_UP被调用...但是,当调用ACTION_DOWN并且我移开手指时(外面)按钮并抬起,ACTION_UP仍然被调用。它应该将图像恢复正常,不做任何其他事情。

twitterBtn.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent touchEvent) {
                switch(touchEvent.getAction())
                {
                    case MotionEvent.ACTION_DOWN:
                        twitterBtn.setBackgroundResource(R.drawable.logintwittertouched);
                        return true;
                    case MotionEvent.ACTION_CANCEL:
                        twitterBtn.setBackgroundResource(R.drawable.logintwitter);
                        return true;
                    case MotionEvent.ACTION_UP:
                        twitterBtn.setBackgroundResource(R.drawable.logintwitter);
                        try {
                            adapter.authorize(MainActivity.this, Provider.TWITTER);
                            dialog = ProgressDialog.show(MainActivity.this, "", "Connecting to Twitter", true);
                        } catch (Exception e) {
                            Log.v(getString(R.string.clickrerr), "" + e.toString());
                        }
                    return true;
                }
                return false;

            }

        });

1 个答案:

答案 0 :(得分:1)

以XML格式执行:

  1. 创建res/drawable/twitter_button.xml

    <item android:drawable="@drawable/twitter_button_on" android:state_pressed="true"/>
    <item android:drawable="@drawable/twitter_button_off"/>
    

    它使用了2张图片:twitter_button_on.pngtwitter_button_off.png,您也应将其放在drawable文件夹中。

  2. 在您的布局中,将其添加到<Button>

    android:background="@drawable/twitter_button"
    
相关问题