【问题标题】:How to Implement onClick and onTouch in Button? [duplicate]如何在Button中实现onClick和onTouch? [复制]
【发布时间】:2014-04-30 07:23:19
【问题描述】:

我有一个按钮以编程方式创建,我需要同时实现 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,当我与按钮交互时如何解决这个问题。

【问题讨论】:

标签: android button touch-event


【解决方案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;

        }
       });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-27
    • 1970-01-01
    • 2021-12-31
    • 2023-02-13
    • 1970-01-01
    • 2013-11-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多