2011-11-14 26 views
0

以及每週在學校,我們正在一個不同的平臺上每週製作一個計算器(wp7,javascript,iphone,android),今天它的android,所以我有一個方法,接收所有擊鍵並根據鍵的值我的類做一些事情,以獲得在C#中的按鈕的值是發件人參數,在JavaScript這個,在android?得到的按鈕,發射事件

private OnClickListener leclicke= new OnClickListener() { 

    public void onClick(View v) { 
    //get the id of the button that fired the click event 
    //findViewById(R.id.???); 
    } }; 

謝謝。

+1

use View.getId(); http://developer.android.com/reference/android/view/View.html#getId() – confucius

回答

2
private OnClickListener leclicke= new OnClickListener() { 

    public void onClick(View v) { 
    //get the id of the button that fired the click event 
    int id = v.getId(); 
    } }; 

,那麼你必須檢查這種觀點有一個ID或者如果你想使用相同的OnClickListener所有按鈕無法使用此

if(id == View.NO_ID){ 
//this view does not have an id 
} 
else{ 
//the view has an id 
} 
1

調用方法getId()

v.getId(); 
+0

它的工作!謝謝! – elios264

0

,那麼你可以這樣做:

Button b1 = (Button)findViewById(R.id.button1); 
Button b1 = (Button)findViewById(R.id.button2); 

private OnClickListener leclicke= new OnClickListener() { 

    public void onClick(View v) { 

     if(v.equals(b1)) { 
     // handle button 1 
     } else if(v.equals(b2)) { 
     // handle button 2 
     } // etc. 
    } }; 

但這有點笨拙。您可以做的另一件事是爲每種方法指定一個單獨的單擊方法,方法是在UI Designer中設置按鈕的點擊屬性,然後在您的Activity中聲明該方法,例如, public onClickButton1(View v);

相關問題