如何在Button中实现onClick和onTouch?

时间:2014-04-30 07:23:20

标签: android button touch-event

我有一个按钮以编程方式创建我需要同时实现onClick和OnTouch到该按钮我想要实现按钮focus_change

像这样实施

ImageView Settings_Button = new ImageView(this);       
        Settings_Button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {    
                //UtilityClass.focusOnallviews(view);
                Intent newIntent = new Intent(activity.getBaseContext(), ProfileSettingActivity.class);
                activity.startActivity(newIntent);           
            }
        });

Note:当我与按钮交互时,我希望实现state_focusedstate_pressed如何解决此问题。

1 个答案:

答案 0 :(得分:1)

您可以使用OnTouch事件

ImageView Settings_Button = new ImageView(this);       
Settings_Button.setOnTouchListener(new OnTouchListener(){
    @Override
    public boolean onTouch(View arg0, MotionEvent arg1) {
        switch(arg1.getAction()){
           case MotionEvent.ACTION_DOWN:
                     // Button is pressed. Change the button to pressed state here
                break;

          case MotionEvent.ACTION_MOVE:
                     // Button is being touched and swiped
                break;

          case MotionEvent.ACTION_UP:
                     // Button is released. Change the button to unpressed state here.
                    Intent newIntent = new Intent(activity.getBaseContext(),ProfileSettingActivity.class);
                    activity.startActivity(newIntent);     
                break;

        }
       });