2016-02-12 50 views
0

有没有什么办法可以根据点击它的按钮的xml id来更改吐司消息?使用开关更改吐司消息

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(); 
    } 
} 
+6

刚刚从那里取出'findViewById',只是有'情况R.id.spotify:'等,也将努力为你的期望。 – EpicPandaForce

+0

它表示Toast.makeText行不可访问:/它还表示从不使用Toast字符串。你能帮我解决这个问题吗? –

+1

是的,把这条线放在switch语句之外。 – EpicPandaForce

回答

4

只使用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(); 
} 
+0

它说,Toast.makeText行不可访问:/它也说,从不使用吐司字符串。你能帮我解决这个问题吗? –

+0

再次编辑检查 –