2014-04-30 45 views
-1

我有一个按钮以编程方式创建我需要实现onClick和OnTouch的那个按钮在同一时间我想实现按钮focus_change如何在Button中实现onClick和onTouch?

上午实现这样

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当我与按钮交互怎样才能解决这个问题。

+0

检查这个1: http://stackoverflow.com/questions/4617898/how-can-i-give-imageview-click-effect-like -a-button-in-android – bardao

+0

为什么要添加ontouch并点击事件? –

回答

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; 

     } 
     });