我正在使用Android Studio。我想在我的應用程序中使用Toast
來顯示「正確」或「不正確」的答案。如何顯示Toast
消息?下面是我的代碼:如何在Android中顯示Toast正確的應用程序?
public class LayarKuisUmumNo1 extends Activity {
Button bt;
TextView tv;
RadioGroup rg;
RadioButton rbu1, rbu2, rbu3, rbu4;
public static String question[] = { "Negara terluas keempat di dunia"};
String answer[] = { "Amerika"};
String opts[] = { "Rusia", "Australia", "Amerika", "Indonesia" };
int position = 0;
public static int correct;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_layar_kuis_umum_no1);
bt = (Button) findViewById(R.id.btnextu1);
tv = (TextView) findViewById(R.id.pertanyaanu1);
rg = (RadioGroup) findViewById(R.id.radioGroup2);
rbu1 = (RadioButton) findViewById(R.id.rbu1a);
rbu2 = (RadioButton) findViewById(R.id.rbu1b);
rbu3 = (RadioButton) findViewById(R.id.rbu1c);
rbu4 = (RadioButton) findViewById(R.id.rbu1d);
tv.setText(question[position]);
rbu1.setText(opts[position]);
rbu2.setText(opts[position + 1]);
rbu3.setText(opts[position + 2]);
rbu4.setText(opts[position + 3]);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
RadioButton selectedans = (RadioButton) findViewById(rg.getCheckedRadioButtonId());
String selectedanstext = selectedans.getText().toString();
if (selectedanstext == answer[position]) {
correct++;
}
position++;
if (position < question.length) {
tv.setText(question[position]);
rbu1.setText(opts[position * 4]);
rbu2.setText(opts[position * 4 + 1]);
rbu3.setText(opts[position * 4 + 2]);
rbu4.setText(opts[position * 4 + 3]);
} else {
Intent in = new Intent(getApplicationContext(), LayarNilaiUmum1.class);
startActivity(in);
}
}
});
}
}
你的英語就可以了,沒問題:)。只需添加Toast.makeText(context,「HELLO」,Toast.LENGTH_LONG).show(); – Opiatefuchs
請閱讀文檔或在互聯網上搜索,因爲許多問題的答案是存在的。檢查字符串不要在java中使用'==',請使用'.equals()' –
[如何在Android中顯示Toast?](http://stackoverflow.com/questions/3500197/how-to -display-toast-in-android) – cafebabe1991