안드로이드 onClick 이벤트에 대한 전망
-
21-12-2019 - |
문제
나는 아이디어가 설정 android:onClick="myClickMethod"
에 대한 여러 텍스트 뷰습니다.
<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"/>
할 수 있는 방법에서 차이가 있는 뷰 myClickMethod()를 호출?
해결책
당신은 그것을 할 수 있을 사용하여 아이디의 각각의 텍스트 보기.사용하여 스위치-케이스 내부의 myClickMethod 에 기반 텍스트 보기 Id 습니다.도 구분할 수 있는 텍스트보기로 태그습니다.
다른 팁
당신은 설정할 수 있습니다 tag
니다.이 방법은,예를 들어,첫 번째 View
사 android:tag="1"
그래서.
에 onClick
방법,단순히 사용 v.getTag()
고 당신이를 구별할 수 있습니다.
당신이 사용할 수 있는 간단한 케이스 스위치
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;
}
}
를 할 때 함수에서 활동은 다음과 같:
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
}
}
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;
}
}
당신이 사용할 수 있는 switch-case
을 결정하는 Textview
를 누르면 비교하여 그들의 id
.
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;
}
}
제휴하지 않습니다 StackOverflow