2017-10-23 15 views
1

私は小さな選択フォームのようなことをしたいと思います。Android - Radiobuttonイベントをクリックして新しいアクティビティに移動します

私は最初のラジオグループの1つを選択し、2番目のラジオグループの1つを選択すると、新しい活動に私を連れて行くクリックイベントをしたいと思います。 私は2つの放射性グループを持っています。

<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical"> 
     <RadioButton android:id="@+id/physic" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="physic" 
      android:onClick="onRadioButtonClicked"/> 
     <RadioButton android:id="@+id/math" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="math" 
      android:onClick="onRadioButtonClicked"/> 
</RadioGroup> 
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical"> 
     <RadioButton android:id="@+id/theories" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="theories" 
      android:onClick="onRadioButtonClicked"/> 
     <RadioButton android:id="@+id/problems_solving" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="problem solving" 
      android:onClick="onRadioButtonClicked"/> 
</RadioGroup> 

私は私のボタンを宣言し、以下のようにonRadioButtonClicked使用しようとした:

public void onRadioButtonClicked(View view) { 
     boolean checked = ((RadioButton) view).isChecked(); 

    switch(view.getId()) { 
     case R.id.math: 
      if (checked) 
       switch(view.getId()) { 
        case R.id.problems_solving: 
         if (checked) 
          showFirstWord("math problem resolution"); 
         break; 
        case R.id.theories: 
         if (checked) 
          showSecondWord("math theories"); 
         break; 
       } 
      break; 

     case R.id.physic: 
      if (checked) 
       switch(view.getId()) { 
        case R.id.problems_solving: 
         if (checked) 
          showThirdWord("physic problem solving"); 
         break; 
        case R.id.theories: 
         if (checked) 
          showFourthWord("physic theories"); 
         break; 
       } 
      break; 
    } 
} 

私は関数内の文字列は、以下のような他の活動にテキストビューに表示する:

private void showFirstWord (String text) { 
    Intent first_word = new Intent(this, FirstActivity.class); 
    first_word.putExtra("key", text); 
    startActivity(first_word); 
} 
private void showSecondWord (String text) { 
    Intent second_word = new Intent(this, SecondActivity.class); 
    second_word.putExtra("key", text); 
    startActivity(second_word); 
} 
private void showThirdWord (String text) { 
    Intent third_word = new Intent(this, ThirdActivity.class); 
    third_word.putExtra("key", text); 
    startActivity(third_word); 
} 
private void showFourthWord (String text) { 
    Intent fourth_word = new Intent(this, FourthActivity.class); 
    fourth_word.putExtra("key", text); 
    startActivity(fourth_word); 
} 

Androidデベロッパーからこのページをフォローしようとしましたが、それでも何をすればよいか分かりません:

私の方法は正しいとは思われません。他のアクティビティに表示される文字列を取得できません。今の私の理性は大丈夫ですか、別の方法を勉強すべきですか?

感謝:)

+0

1から4の機能が何であるか分かりません。ただし、クリックされたビューはRadioGroup(ViewGroupは常にView)*または*クリックされたViewはRadioButtonです。 view.getId()は== R.id.firstGroupと同時に== R.id.answer1_1にすることはできません。しかしそれは意図の質問であなたを助けません。この質問に答えるために、私は関数が何をするのかを見ていく必要があります。 – 0X0nosugar

+0

申し訳ありません私は少し明確にしたいことを作ってみました。もう少し理解できるといいなあと思います。 :) –

答えて

0

あなただけの最初のsubjectSelectedと呼ばれるString変数を作成してコードonRadioButtonClickedを簡素化することができます。その後、

private String subjectSelected = ""; 
public void onRadioButtonClicked(View view) { 
    RadioButton radioButton = (RadioButton) view; 

    switch(view.getId()) { 
     case R.id.math: 
      subjectSelected = radioButton.getText().toString(); 
      break; 
     case R.id.physic: 
      subjectSelected = radioButton.getText().toString(); 
      break; 
     case R.id.problems_solving: 
      if (subjectSelected.equals("math")) { 
       showFirstWord ("math problem resolution"); 
      } else if (subjectSelected.equals("physic")) { 
       showThirdWord("physic problem solving"); 
      } 
      break; 
     case R.id.theories: 
      if (subjectSelected.equals("math")) { 
       showSecondWord("math theories"); 
      } else if (subjectSelected.equals("physic")) { 
       showFourthWord("physic theories"); 
      } 
      break; 
    } 
} 

、あなたが別のアクティビティに渡すテキストを表示します。 keyの値を取得するにはBundleを使用してください。

例えば:

public class FirstActivity extends AppCompatActivity { 

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

     Bundle bundle = getIntent().getExtras(); 
     String key = bundle.getString("key"); 

     TextView textView = (TextView) findViewById(R.id.textView1); // Replace the textView1 with the id you set to your textview. 
     textView.setText(key); 
    } 
} 

あなたのFirstActivityのコードをコピーして、キーを得るためにあなたのSecondActivityThirdActivityFourthActivityに貼り付けることができます。

+0

ありがとう!それは本当に私がやろうとしていたことを単純化します! :)私は最初の答えの文字列を使用するとは思わなかった! –

0
String str; // store the text corresponding to the RadioButton which is clicked 

    switch(view.getId()) { 
       case R.id.radioButton1: 
        if (checked) 
        str = "button1Text"; 
        break; 
       case R.id.radioButton2: 
        if (checked) 
        str = "button2Text"; 
        break; 
       case R.id.radioButton3: 
        if (checked) 
        str = "button3Text"; 
        break; 
     } 

      Intent intent = new Intent(this, WinderDTActivity.class); 
      intent.putExtra("radioChosen", str); // pass "str" to the next Activity 
+0

私はそれを実装しようとしたアドバイスをありがとう、それは私に私の文字列を表示させるための別の方法を示した! –

関連する問題