[英]How to make radio buttons display a text in another activity when clicked in android studio?
I have 6 options that the user can select from and a button that takes them to the next page when clicked.我有 6 个选项,用户可以从中选择 select,还有一个按钮可以在单击时将它们带到下一页。 I have two pages like this.我有两个这样的页面。 After one choice from each page is selected, I would like to display certain text depending on the radio buttons clicked previously, in another activity.从每个页面中选择一个选项后,我想根据之前单击的单选按钮在另一个活动中显示某些文本。 How can I do this in java in android studio?如何在 android 工作室的 java 中执行此操作?
If you are controlling the FragmentManager
you can just pass the options as an argument to the next Fragment
's constructor otherwise, you can save everything in a static variable (as long as no Views are in there, so don't store the radio buttons there) and access that variable from outside.如果您正在控制FragmentManager
,则可以将选项作为参数传递给下一个Fragment
的构造函数,否则,您可以将所有内容保存在 static 变量中(只要其中没有视图,所以不要存储单选按钮那里)并从外部访问该变量。
Like this:像这样:
public class MyFragment {
public static boolean isRadioButtonXPressed = false; //change to true if it's pressed by default
public void onCreate(...) {
radioButtonX.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
isRadioButtonXPressed = checked;
}
});
}
}
//from the other fragment
public class MyFragment2 {
public void onCreateView(...) {
if (MyFragment.isRadioButtonXPressed) {
//it's been pressed
} else {
//it's not been pressed
}
}
}
There are many different ways.有很多不同的方法。
As you are using activity, you can use Intent to pass data.当您使用活动时,您可以使用 Intent 来传递数据。 Here is a sample:这是一个示例:
Intent page = new Intent(FirstActivity.this, SecondActivity.class);
page.putExtra("key", "This is my text");
startActivity(page);
For getting the value on Second Activity onCreate() method:要获取 Second Activity onCreate() 方法的值:
String value = getIntent().getStringExtra("key");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.