【发布时间】:2016-02-12 07:10:24
【问题描述】:
有什么方法可以根据单击它的按钮的 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();
}
}
【问题讨论】:
-
只需从那里删除
findViewById并只保留case R.id.spotify:等,它就会按您的预期工作。 -
表示无法访问 Toast.makeText 行:/ 它还表示从未使用过 toast 字符串。你能帮我解决这个问题吗?
-
是的,把那行放在 switch 语句之外。
-
谢谢!!!第一次实际使用开关...初学者错误:D
标签: java android android-toast