将单选按钮数据发送到下一个活动(代码片段)

author author     2022-12-28     585

关键词:

我试图让用户输入他们的名字,然后点击三个单选按钮中的一个,然后点击提交按钮。在下一个活动中,它将显示他们的名字和他们选择的单选按钮。我已经设法能够发送名称,但我不知道如何发送单选按钮选择。有人可以帮忙吗?

这是我在主活动布局.xml中的内容

<EditText
    android:id="@+id/operatorName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="25dp"
    android:ems="10"
    android:hint="@string/operator_name" />

<RadioGroup
    android:id="@+id/radioShifts"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_marginTop="10dp" >
    <RadioButton
        android:id="@+id/radioButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/radio1"
        android:checked="true"
        android:onClick="onRadioButtonClicked" />
    <RadioButton
        android:id="@+id/radioButton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/radio2"
        android:onClick="onRadioButtonClicked" />
    <RadioButton
        android:id="@+id/radioButton3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/radio3"
        android:onClick="onRadioButtonClicked" />
</RadioGroup>

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="28dp"
    android:text="@string/button1"
    android:onClick="onButton1" />

我在main_activity .java文件中有这个:

    public final static String OP_NAME = "com.cyapps.downtimer.OPNAME";

    @Override
    public void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    

    public void onRadioButtonClicked(View view) 
        // Is the button now checked?
        boolean checked = ((RadioButton) view).isChecked();
        // Check which radio button was clicked
        switch(view.getId()) 
            case R.id.radioButton1:
                if (checked)
                    break;
            case R.id.radioButton2:
                if (checked)
                    break;
            case R.id.radioButton3:
                if (checked)
                    break;
        
/*        Intent intent = new Intent(this, WinderDTActivity.class);
        EditText button = (EditText) findViewById(RadioGroup.getCheckedRadioButtonId());
        String radioChosen = button.getText().toString();
        intent.putExtra(RADIO_CHOSEN, radioChosen);*/
    

    public void onButton1(View view) 
        Intent intent = new Intent(this, WinderDTActivity.class);
        EditText editText = (EditText) findViewById(R.id.operatorName);
        String opName = editText.getText().toString();
        intent.putExtra(OP_NAME, opName);
        startActivity(intent);
    

/ * * /中的代码是我认为我应该做的..但我不确定。有人帮忙吗?我真的很感激..

答案
String str; // store the text corresponding to  the RadioButton which is clicked 

   switch(view.getId()) 
                case R.id.radioButton1:
                    if (checked)
                     str = "button1Text";
                        break;
                case R.id.radioButton2:
                    if (checked) str = "button2Text";
                        break;
                case R.id.radioButton3:
                    if (checked) str = "button3Text";
                        break;
         

            Intent intent = new Intent(this, WinderDTActivity.class);
            intent.putExtra("radioChosen", str); // pass "str" to the next Activity

编辑:要在下一个活动中接收数据,请使用

Bundle extras = getIntent().getExtras();
if (extras != null) 
    String message= extras.getString("radioChosen");


另一答案

- 使用switch选择单选按钮。

switch (types.getCheckedRadioButtonId()) 
      case R.id.sit_down:
        intent.putExtra(SIT_DOWN, "sitdown");
        break;
      case R.id.take_out:
        intent.putExtra(TAKE_OUT, "takeout");
        break;

      

- 现在就做startActivity(intent);

Android Studio:将单选按钮字符串添加到数据库

】AndroidStudio:将单选按钮字符串添加到数据库【英文标题】:AndroidStudio:Addradiobuttonstringtodatabase【发布时间】:2017-09-2702:40:42【问题描述】:我正在创建一个应用程序-在AndroidStudio中-人们需要填写表格。现在我可以将所有数据放... 查看详情

如何使用 xampp 将单选按钮值提交到数据库

】如何使用xampp将单选按钮值提交到数据库【英文标题】:howtosubmitradiobuttonvaluestodatabaseusingxampp【发布时间】:2018-03-2400:51:31【问题描述】:我正在尝试将已选择的单选按钮值提交到数据库,但我无法这样做。谁能帮帮我?我浏... 查看详情

我需要帮助将单选按钮存储在 mysql 数据库中[关闭]

】我需要帮助将单选按钮存储在mysql数据库中[关闭]【英文标题】:Ineedhelptostoreradio-buttoninmysqldatabase[closed]【发布时间】:2013-07-0816:41:27【问题描述】:我正在做一个简单的调查,我需要将答案存储在mysql数据库中。(我正在使用... 查看详情

将单选组分配给单选按钮

】将单选组分配给单选按钮【英文标题】:AssignRadioGrouptoRadioButtons【发布时间】:2020-04-1605:44:28【问题描述】:我有几个单选按钮我需要为其分配一个单选组,以便一次只能选择一个单选按钮。没有单选组选项。我已经尝试过为... 查看详情

多个单选按钮选择(代码片段)

...个单选按钮,每当单选按钮被选中时,我想在提交表单前将单选按钮的值添加到一个数组中。请指导我如何才能实现这个目标,谢谢帮助。<form[formGroup]="Form"(ngSubmit)="submitForm()"><divclass="btn-group"ngbRadioGroupna 查看详情

使用 PHP 将单选按钮值添加到 MYSQL 表

】使用PHP将单选按钮值添加到MYSQL表【英文标题】:AddingradiobuttonvaluestoMYSQLtableusingPHP【发布时间】:2013-03-2922:26:16【问题描述】:我正在使用PHP/MYSQL创建一个包含单选按钮的表单。我正在尝试将选中的单选按钮的值添加到数据库... 查看详情

将单选按钮关联到各个组

】将单选按钮关联到各个组【英文标题】:Associatingradiobuttonstoindividualgroups【发布时间】:2017-10-0805:26:32【问题描述】:我在一个表中有几行。每行有两个单选按钮,它们应该只相互关联,但不与其他行中的单选按钮关联。提交... 查看详情

使用 JS 或 JQUERY 将单选按钮转换为只读(未禁用)

】使用JS或JQUERY将单选按钮转换为只读(未禁用)【英文标题】:Convertradiobuttontoreade-only(notdisable)withJSorJQ【发布时间】:2021-06-2807:02:41【问题描述】:我有一个参数,其中包含三个输入选项作为单选按钮(例如“是”;“否”;... 查看详情

如何将单选按钮文本更改为 int?

】如何将单选按钮文本更改为int?【英文标题】:HowtochangeRadioButtontextintoint?【发布时间】:2012-11-0704:03:45【问题描述】:我有一个广播组,有2个选项,即男性和女性。我想将RadioButtonMale作为intnumber"1"和Female作为"2"保存到我的数... 查看详情

如何将单选按钮值与输入字段相关联

】如何将单选按钮值与输入字段相关联【英文标题】:HowtoassociateRadioButtonvaluewithInputField【发布时间】:2016-03-0715:45:27【问题描述】:我正在创建一个测验应用程序...我创建了一个问题文本区域,以及一些选项输入字段..每个输... 查看详情

如何从 PHP 中的单选按钮发送值属性 [关闭]

...【发布时间】:2013-01-2218:10:03【问题描述】:我正在努力将单选按钮的值发送到电子邮件。我编写了2个单选按钮,我将第一个设置为默认选中。表单和值有效,但未提交单选按钮值。有什么妙语吗?【问题讨论】:没有您的代... 查看详情

不将数据发送到下一个活动,而是发送到之后的活动(使用 parcelable)

】不将数据发送到下一个活动,而是发送到之后的活动(使用parcelable)【英文标题】:Senddatanottonextactivity,buttheoneafter(usingparcelable)【发布时间】:2018-04-2217:10:17【问题描述】:假设在活动A(得分)中,我创建了自定义类Player的... 查看详情

将单选按钮从一个 PHP 表单添加到另一个 PHP 表单

】将单选按钮从一个PHP表单添加到另一个PHP表单【英文标题】:AddingaRadiobuttonfromonePHPformtoanotherPHPform【发布时间】:2017-08-0401:40:34【问题描述】:基本上,我有两个表单,一个UserForm和AdminForm。在AdminForm上,我希望有一个文本字... 查看详情

MVVM:将单选按钮绑定到视图模型?

】MVVM:将单选按钮绑定到视图模型?【英文标题】:MVVM:Bindingradiobuttonstoaviewmodel?【发布时间】:2011-01-1802:10:43【问题描述】:编辑:问题已在.NET4.0中修复。我一直在尝试使用IsChecked按钮将一组单选按钮绑定到视图模型。在查看... 查看详情

无法将单选按钮对齐到中心(垂直)[重复]

】无法将单选按钮对齐到中心(垂直)[重复]【英文标题】:Cannotalignradiobuttontocenter(vertically)[duplicate]【发布时间】:2019-03-0913:27:27【问题描述】:我创建了一个由两列组成的结构,第一列有三个单选按钮,我需要对齐到中心(... 查看详情

使用 Bootstrap,如何将单选输入显示为按钮?

】使用Bootstrap,如何将单选输入显示为按钮?【英文标题】:WithBootstrap,howdoIshowradioinputsasbuttons?【发布时间】:2016-04-1202:12:54【问题描述】:我的表单目前包含以下代码:<linkhref="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap... 查看详情

如何将单选按钮创建为按钮?

】如何将单选按钮创建为按钮?【英文标题】:Howtocreateradiobuttonasbuttons?【发布时间】:2011-07-0312:03:39【问题描述】:如何创建常规单选按钮,使其与使用css样式的普通按钮一样?这是我的单选按钮:<divclass="radio-toolbar"><i... 查看详情

如何通过jquery将单选按钮的值与类合并

】如何通过jquery将单选按钮的值与类合并【英文标题】:howtomergevalueofradiobuttonwithclassthroughjquery【发布时间】:2018-10-1412:34:23【问题描述】:$check=$(this).closest(\'ul\').parent().attr(\'id\');我有一个“check”变量,其值为“quiz1”并且这... 查看详情