如何在 Android Studio 中单击单选按钮时在另一个活动中显示文本?

huangapple go评论131阅读模式
英文:

How to make radio buttons display a text in another activity when clicked in android studio?

问题

我有6个选项供用户选择,并有一个按钮,当用户点击时,会进入下一页。我有两个类似的页面。在每个页面中选择一个选项后,我想根据先前单击的单选按钮,在另一个活动中显示特定文本。我如何在Android Studio中使用Java来实现这个功能?

英文:

I have 6 options that the user can select from and a button that takes them to the next page when clicked. 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?

答案1

得分: 0

如果您正在控制FragmentManager,可以将选项作为参数传递给下一个Fragment的构造函数;否则,您可以将所有内容保存在静态变量中(只要不包含视图,所以不要将单选按钮存储在那里),然后从外部访问该变量。

像这样:

  1. public class MyFragment {
  2. public static boolean isRadioButtonXPressed = false; // 默认情况下更改为true(如果已按下)
  3. public void onCreate(...) {
  4. radioButtonX.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  5. @Override
  6. public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
  7. isRadioButtonXPressed = b;
  8. }
  9. });
  10. }
  11. }
  12. // 来自另一个Fragment
  13. public class MyFragment2 {
  14. public void onCreateView(...) {
  15. if (MyFragment.isRadioButtonXPressed) {
  16. // 已按下
  17. } else {
  18. // 未按下
  19. }
  20. }
  21. }
  22. 有许多不同的方法
英文:

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.

Like this:

  1. public class MyFragment {
  2. public static boolean isRadioButtonXPressed = false; //change to true if it's pressed by default
  3. public void onCreate(...) {
  4. radioButtonX.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  5. @Override
  6. public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
  7. isRadioButtonXPressed = checked;
  8. }
  9. });
  10. }
  11. }
  12. //from the other fragment
  13. public class MyFragment2 {
  14. public void onCreateView(...) {
  15. if (MyFragment.isRadioButtonXPressed) {
  16. //it's been pressed
  17. } else {
  18. //it's not been pressed
  19. }
  20. }
  21. }

There are many different ways.

答案2

得分: 0

你可以使用Intent来传递数据,因为你正在使用activity。以下是一个示例:

  1. Intent page = new Intent(FirstActivity.this, SecondActivity.class);
  2. page.putExtra("key", "这是我的文本");
  3. startActivity(page);

在Second Activity的onCreate()方法中获取值:

  1. String value = getIntent().getStringExtra("key");
英文:

As you are using activity, you can use Intent to pass data. Here is a sample:

  1. Intent page = new Intent(FirstActivity.this, SecondActivity.class);
  2. page.putExtra("key", "This is my text");
  3. startActivity(page);

For getting the value on Second Activity onCreate() method:

  1. String value = getIntent().getStringExtra("key");

huangapple
  • 本文由 发表于 2020年8月4日 23:32:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/63250226.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定