java - Android - 单选按钮单击事件转到新 Activity

标签 java android

我想做一些诸如小型选择表格之类的事情。

我想做一个点击事件,如果我选择第一个 radio 组中的一个和第二个 radio 组中的另一个,它会将我带到一个新 Activity 。 我有两个单选组,每个组内有两个单选按钮。

<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;
    }
}

我希望函数中的字符串显示在其他 Activity 的 TextView 中,如下所示:

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

我的方法似乎不正确,因为我无法让字符串出现在其他 Activity 中。我的推理现在可以吗还是我应该研究其他方法?

谢谢:)

最佳答案

您可以简化代码onRadioButtonClicked,只需首先创建一个名为subjectSelectedString变量。

然后:

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;
    }
}

并显示您传递给另一个 Activity 的文本。使用Bundle获取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);
    }
}

您可以复制 FirstActivity 的代码并粘贴到 SecondActivityThirdActivityFourthActivity 来获取 key 。

关于java - Android - 单选按钮单击事件转到新 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46897005/

相关文章:

java - Mule 中的FunctionalTestCase - 如何在另一个流程之前加载一个流程?

android - 如何在 android listview 中从 json 动态创建 View ?

android - "NoSuchMethodError: The method ' [] ' was called on null."我的流中出现错误

java - 使用 JACKSON 解析 python 字典

java - Quartz 调度程序在作业执行前关闭

android 服务通知 Activity 完成的最佳方式?

android - 如何使工具栏 Logo 在android中可点击

java - 将 DriveId 转换为人类可读的路径

java - 我想在不直接使用 JavaScript 的情况下制作一个 "Web 2.0"应用程序

java - 部署java servlets : how to atomatically do the .类文件复制并重启tomcat