2016-03-18 57 views
1

我正在使用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); 
      } 
     } 
    }); 
} 
} 
+0

你的英語就可以了,沒問題:)。只需添加Toast.makeText(context,「HELLO」,Toast.LENGTH_LONG).show(); – Opiatefuchs

+1

請閱讀文檔或在互聯網上搜索,因爲許多問題的答案是存在的。檢查字符串不要在java中使用'==',請使用'.equals()' –

+1

[如何在Android中顯示Toast?](http://stackoverflow.com/questions/3500197/how-to -display-toast-in-android) – cafebabe1991

回答

0

這裏是我的代碼:

public class MainActivity extends Activity { 

    RadioGroup rg; 
    RadioButton rbut1; 
    Button bt; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     rg = (RadioGroup) findViewById(R.id.radioGroup1); 

     bt = (Button) findViewById(R.id.button1); 

     bt.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       int selectedId = rg.getCheckedRadioButtonId(); 
       rbut1 = (RadioButton) findViewById(selectedId); 
       Toast.makeText(MainActivity.this, rbut1.getText().toString(), 
         Toast.LENGTH_SHORT).show(); 
      } 
     }); 

    } 
} 
+0

我想說謝謝你。現在我的應用正在工作。 –

0

的代碼添加吐司消息是這樣的,

Toast.makeText(this, "ZOOM Or use Landscape mode", 
      Toast.LENGTH_SHORT).show(); 

基本上,第一個參數是上下文,第二個是包含要顯示信息的字符串最後一個參數是您希望顯示消息的持續時間。

相關問題