使用开关

时间:2016-02-12 07:10:25

标签: java android android-toast

有什么办法可以根据点击它的按钮的xml id更改toast消息吗?

public void toast (View view) {
    String toast ="";
    switch (view.getId()) {
        case findViewById(R.id.spotify): toast = "Spotify streamer will show here";
            break;
        case findViewById(R.id.scores):  toast = "Scores app will show here";
            break;
        case findViewById(R.id.library):  toast = "March";
            break;
        case findViewById(R.id.build):  toast = "Build app will show here";
            break;
        case findViewById(R.id.xyz):  toast = "XYZ Reader app will show here";
            break;
        case findViewById(R.id.capstone):  toast = "My Capstone app will show here";
            break;
        Toast.makeText(this, toast, Toast.LENGTH_SHORT).show();
    }
}

2 个答案:

答案 0 :(得分:4)

只需使用case R.id.spotify:代替case findViewById(R.id.spotify):等...

答案 1 :(得分:2)

public void toast (View view) {
String toast ="";
switch (view.getId()) {
    case R.id.spotify: toast = "Spotify streamer will show here";
        break;
    case R.id.scores:  toast = "Scores app will show here";
        break;
    case R.id.library:  toast = "March";
        break;
    case R.id.build:  toast = "Build app will show here";
        break;
    case R.id.xyz:  toast = "XYZ Reader app will show here";
        break;
    case R.id.capstone:  toast = "My Capstone app will show here";
        break;
   }
   Toast.makeText(this, toast, Toast.LENGTH_SHORT).show();
}