2014-02-25 41 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; 
    } 
}