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

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

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的构造函数;否则,您可以将所有内容保存在静态变量中(只要不包含视图,所以不要将单选按钮存储在那里),然后从外部访问该变量。

像这样:

public class MyFragment {
    public static boolean isRadioButtonXPressed = false; // 默认情况下更改为true(如果已按下)

    public void onCreate(...) {
        radioButtonX.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                isRadioButtonXPressed = b;
            }
        });
    }
}

// 来自另一个Fragment
public class MyFragment2 {
    public void onCreateView(...) {
        if (MyFragment.isRadioButtonXPressed) {
            // 已按下
        } else {
            // 未按下
        }
    }
}
有许多不同的方法
英文:

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:

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.

答案2

得分: 0

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

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

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

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

As you are using activity, you can use Intent to pass data. 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:

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:

确定