Android - Radiobutton点击事件转到新活动

时间:2017-10-23 19:22:57

标签: java android

我想做一些像小选择的形式。

我想做一个点击事件,如果我选择第一个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/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开发者那里关注此页面,但我仍然不确定如何处理它:https://stuff.mit.edu/afs/sipb/project/android/docs/guide/topics/ui/controls/radiobutton.html

我的方法似乎不正确,我不能让字符串出现在其他活动中。我的理由现在还是可以,还是我应该学习另一种方法?

谢谢:)

2 个答案:

答案 0 :(得分:0)

sentence

答案 1 :(得分:0)

您可以简化代码onRadioButtonClicked,只需先创建名为String的{​​{1}}变量。

然后:

subjectSelected

并显示您传递给其他活动的文本。使用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; } } 获取Bundle的价值。

e.g:

key

您可以复制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); } } 的代码并粘贴到FirstActivitySecondActivityThirdActivity以获取密钥。