gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 11:00:58 25 4
gpt4 key购买 nike

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

我想做一个点击事件,如果我选择第一个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com