[英]How do I rewrite if into switch with cases as strings in android app?
我正在检测我单击了哪个按钮:
public void next_page(View view){
//if else - works:
if( view.getId() == "fly_button")
Log.d(LOG, " fly button clicked");
else if (view.getId() == "imageButton")
Log.d(LOG, "image button clicked");
//switch does not work:
// every id is a string, how to show it as integer in "case"?
switch( view.getId() ){
case "fly_button":
Log.d(LOG, "fly button clicked");
case "imageButton":
Log.d(LOG, "image button clicked");
}
请使用此:
public void onClick(View v) {
switch(v.getId()){
case R.id.Button1:
// statements
break;
case R.id.Button2:
// statements
break;
}
}
如果这不起作用,请尝试以下操作:
int id = view.getId();
if (id == R.id.Button1) {
action1();
} else if (id == R.id.Button2) {
action2();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.