2014-04-30 44 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; 

     } 
     });