2010-08-08 47 views
0

我的應用程序偵聽來自觸摸屏和按鈕的用戶輸入,但我想傾聽與主屏幕視圖中特定項目的交互。所以,例如,我需要聽取用戶點擊菜單按鈕,然後輸出。有沒有可以通過onClick()類的參數?或者我需要採取另一條路線?如何聆聽特定的按鈕按鈕和主視圖用戶交互?

+1

什麼語言/平臺您使用? – Oded 2010-08-08 08:04:04

+0

java在android sdk中 – 2011-01-28 17:20:22

回答

0
public class Mtest extends Activity { 
    Button b1; 
    Button b2; 
    public void onCreate(Bundle savedInstanceState) { 
    ... 
    b1 = (Button) findViewById(R.id.b1); 
    b2 = (Button) findViewById(R.id.b2); 
    b1.setOnClickListener(myhandler1); 
    b2.setOnClickListener(myhandler2); 
    ... 
    } 
    View.OnClickListener myhandler1 = new View.OnClickListener() { 
    public void onClick(View v) { 
     // it was the 1st button 
    } 
    }; 
    View.OnClickListener myhandler2 = new View.OnClickListener() { 
    public void onClick(View v) { 
     // it was the 2nd button 
    } 
    }; 
} 

或者,如果你只用一個clicklistener工作,你可以這樣做:

View.OnClickListener myOnlyhandler = new View.OnClickListener() { 
    public void onClick(View v) { 
     switch(v.getId()) { 
     case R.id.b1: 
      // it was the first button 
      break; 
     case R.id.b2: 
      // it was the second button 
      break; 
     } 
    } 
}