简体   繁体   English

在 android 工作室中单击时,如何使单选按钮在另一个活动中显示文本?

[英]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.

相关问题 单击来自两个不同活动的两个单选按钮时,如何在新活动中显示文本? - How to display text in a new activity when two radio buttons from two different activities are clicked? 如何在我在android studio中的第二个活动上显示单击的单选按钮值? - how to display the clicked radio button value on my second activity in android studio? Android-如何在另一个活动中向单选按钮添加单选按钮? (以编程方式) - Android - How to add radio buttons to a radiogroup in another Activity? (programatically) 选中单选按钮时如何在另一个活动中显示EditText输入? - How to display EditText input in another activity when Radio Button is checked? 单击 Cardview 时如何在另一个活动中显示详细信息 - How can I display the details in another activity when Cardview is clicked 单击时如何使Android Buttons在自身上显示动画 - How to make Android Buttons show animation on itself when clicked Android 单击主按钮时显示按钮 - Android display buttons when clicked on main button 如何在运行时在Android中显示图像并在列表中将单选按钮作为单选组 - How to display Images in Android on Runtime & make radio buttons as radio groups in List 在android studio中单击时如何使复选框可编辑 - how to make a checkbox editable when clicked in android studio 当我单击该按钮时,我的应用程序崩溃,这将带我进入Java,Android Studio上的另一个活动 - My app crashes when I clicked the button which will take me the another Activity on Java, Android Studio
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM