2016-02-12 71 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

再次編輯檢查 –