2016-04-18 9 views
0

私はアンドロイドを新しくしました。ラジオボタンがチェックされているなら、次のページに行きたいです。私は以下のコードを書いたが、うまくいきません。 何が問題ですか?RadioButtonがチェックされている場合、次のページに移動するにはどうすればいいですか? Androidプログラミングで

public class second extends Activity { 

    public void onCreate (Bundle shamim){ 


     super.onCreate(shamim); 

     setContentView(R.layout.second); 


     findViewById(R.id.radioButton1); 

     RadioButton radioButton1 = (RadioButton) findViewById(R.id.radioButton1); 


     findViewById(R.id.radioButton2); 
     RadioButton radioButton2 = (RadioButton)findViewById(R.id.radioButton2); 


     if (radioButton1.isChecked()) { 

      startActivity(new Intent(second.this,third.class)); 

      else { (radioButton2.isChecked()) { 


       startActivity(new Intent(second.this,MyActivity.class)); 

      } 

      } 

     } 

    } 
+0

findViewById(R.id.radioButton1)を削除します。およびfindViewById(R.id.radioButton2);これらは必須ではないためです。 –

+0

はradiobutton.setOnCheckedChangeListernerを使用します。 –

答えて

1

isChecked()は、ボタンの現在の状態のみを表示します。状態が変更されたとき(デフォルトではチェックされていない)には通知されないため、ユーザーが実際にボタンをクリックしたときにコードが反応するように設定されていません。

代わりisCheckedを呼び出すのは、おそらくあなたが適切に対応できるように、クリックされたときのいずれかを知るために、各ボタンにリスナーを設定する:

radioButton1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() 
{ 
    @Override 
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 
    { 
     if (isChecked) 
     { 
      startActivity(new Intent(second.this,third.class)); 
     } 
    } 
} 

radioButton2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() 
{ 
    @Override 
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 
    { 
     if (isChecked) 
     { 
      startActivity(new Intent(second.this,MyActivity.class)); 
     } 
    } 
} 
関連する問題