2014-02-25 34 views
0

我有一個想法,爲幾個TextView設置android:onClick="myClickMethod"Android onClick事件查看

<TextView 
    android:id="@+id/search_suggestion_1" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:onClick="myClickMethod"     
    android:clickable="true"/> 

<TextView 
    android:id="@+id/search_suggestion_2" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:onClick="myClickMethod"     
    android:clickable="true"/> 

<TextView 
    android:id="@+id/search_suggestion_3" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:onClick="myClickMethod"     
    android:clickable="true"/> 

我該如何區別TextView中的myClickMethod()被調用?

+0

原來的問題,就是以myClickMethod的TextView的文本() –

回答

2

你可以通過使用每個文本視圖的ID。基於文本視圖標識,在myClickMethod中使用Switch-Case。你也可以通過標籤區分文本視圖。

0

您可以爲每個人設置一個tag。這樣,例如,對於第一個View使用android:tag="1"等等。

在您的onClick方法中,只需使用v.getTag()即可區分它們。

public void onClickMethod(View view){ 
//Input argument is the view on which click is fired. 
//Get id of that view 

int id = view.getId(); 

switch(id){ 
// your logic 
} 
} 
0

當你犯了一個功能活性喜歡,你可以使用一個簡單的開關情況

public void myClickMethod(View v) 
{ 
    switch(v.getId())/
    // id is an int value. use getId to get the id of the view 
    { 
     case R.id.search_suggestion_1: 
      // search suggestion 1 
     break 
     case R.id.search_suggestion_2 : 
      // search suggestion 2 
     break; 
     case R.id.search_suggestion_3: 
      // search suggestion 3 
     break; 
    } 
} 
0

public void myClickMethod(View v) { 

    switch(v.getd()) { 

     case R.id.search_suggestion_1: 
      //code here 
      break; 

     case R.id.search_suggestion_2: 
      //code here 
      break; 

     case R.id.search_suggestion_3: 
      //code here 
      break; 
    } 

} 
0

您可以使用switch-case使該Textview是通過比較它們的id按下決定:

0
public void myClickMethod(View v) 
{ 
    switch(v.getId()) 
    { 

    case R.id.search_suggestion_1: 
    //code here 
    break; 
    case R.id.search_suggestion_2: 
    //code here 
    break; 
    case R.id.search_suggestion_3: 
    //code here 
    break; 
    default: break; 
    } 
}