【问题标题】:change toast message using switch使用开关更改吐司消息
【发布时间】: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


【解决方案1】:

只需使用case R.id.spotify: 而不是case findViewById(R.id.spotify): 等...

【讨论】:

    【解决方案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();
    }
    

    【讨论】:

    • 表示无法访问 Toast.makeText 行:/ 它还表示从未使用过 toast 字符串。你能帮我解决这个问题吗?
    猜你喜欢
    • 2021-10-06
    • 2021-09-06
    • 1970-01-01
    • 2021-10-06
    • 1970-01-01
    • 2015-01-26
    • 1970-01-01
    • 2022-08-18
    • 2011-05-13
    相关资源
    最近更新 更多