I want to change this switch statement to if statement or while loop. How can I do that? Note this piece of code using Android Studio.
public void onClick(View v) {
switch(v.getId()){
case R.id.button:
Log.v(TAG, "verbose");
break;
case R.id.button2:
Log.d(TAG, "Debug");
break;
case R.id.button3:
Log.i(TAG, "Information");
break;
case R.id.button4:
Log.w(TAG, "Warning");
break;
case R.id.button5:
Log.e(TAG,"Error");
break;
}
}
I would suggest you to use existing switch
statement, but if you really want if
, you can do it like this:
public void onClick(View v) {
int id = v.getId();
if (id == R.id.button) {
Log.v(TAG, "Verbose");
} else if (id == R.id.button2) {
Log.d(TAG, "Debug");
} else if (id == R.id.button3) {
Log.i(TAG, "Information");
} else if (id == R.id.button4) {
Log.w(TAG, "Warning");
} else if (id == R.id.button5) {
Log.e(TAG, "Error");
}
}
If you're using AndroidStudio, you could use intent
switch
keyword alt+enter
(on Mac, not sure about Windows) Replace 'switch' with 'if'
Note: You could replace if to switch using the same method.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.