从选定的单选按钮的Andr​​oid获得价值单选、按钮、价值、Andr

2023-09-13 00:09:20 作者:撤了青春弃了年华

我有一张code与RadioGroup中在三个单选按钮。我想设置一个oncheckedlistener,将显示举杯但是我已经变得至今不工作的单选按钮的值。我如何获得单选按钮的值,并举杯显示呢?谢谢你在前进,这是我的code:

 包com.example.toohelps;

进口android.os.Bundle;
进口android.app.Activity;
进口android.view.Menu;
进口android.view.View;
进口android.widget.RadioButton;
进口android.widget.RadioGroup;
进口android.widget.RadioGroup.OnCheckedChangeListener;
进口android.widget.Toast;

公共类MainActivity延伸活动{

RadioGroup中RG;
@覆盖
保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);

    RadioGroup中RG =(RadioGroup中)findViewById(R.id.radioGroup1);
    最终的字符串值=((单选)findViewById(rg.getCheckedRadioButtonId()))的getText()的toString()。

    rg.setOnCheckedChangeListener(新OnCheckedChangeListener()
    {
        公共无效onCheckedChanged(RadioGroup中组,诠释checkedId){

            Toast.makeText(getBaseContext(),价值,Toast.LENGTH_SHORT).show();
        }
    });

}

 }
 

XML文件

 < RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
的xmlns:工具=htt​​p://schemas.android.com/tool​​s
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:paddingBottom会=@扪/ activity_vertical_margin
机器人:以下属性来=@扪/ activity_horizo​​ntal_margin
机器人:paddingRight =@扪/ activity_horizo​​ntal_margin
机器人:paddingTop =@扪/ activity_vertical_margin
工具:上下文=MainActivity。>

< RadioGroup中
    机器人:ID =@ + ID / radioGroup1
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_alignParentTop =真
    机器人:layout_centerHorizo​​ntal =真
    机器人:layout_marginTop =152dp>

    <单选按钮
        机器人:ID =@ + ID / radio0
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT

        机器人:文本=选择1/>

    <单选按钮
        机器人:ID =@ + ID /收音机1
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT

        机器人:文本=选择2/>

    <单选按钮
        机器人:ID =@ + ID / RADIO2
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文本=选择3/>
 < / RadioGroup中>

 < / RelativeLayout的>
 

解决方案

测试和工作。检查这

 进口android.app.Activity;
进口android.os.Bundle;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.widget.Button;
进口android.widget.RadioButton;
进口android.widget.RadioGroup;
进口android.widget.Toast;

公共类MyAndroidAppActivity延伸活动{

  私人RadioGroup中radioSexGroup;
  私人单选radioSexButton;
  私人按钮btnDisplay;

  @覆盖
  公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);

    addListenerOnButton();

  }

  公共无效addListenerOnButton(){

    radioSexGroup =(RadioGroup中)findViewById(R.id.radioSex);
    btnDisplay =(按钮)findViewById(R.id.btnDisplay);

    btnDisplay.setOnClickListener(新OnClickListener(){

        @覆盖
        公共无效的onClick(视图v){

                //获取选定的单选按钮,从RadioGroup中
            INT selectedId = radioSexGroup.getCheckedRadioButtonId();

            //找到单选按钮由归国ID
                radioSexButton =(单选)findViewById(selectedId);

            Toast.makeText(MyAndroidAppActivity.this,
                radioSexButton.getText(),Toast.LENGTH_SHORT).show();

        }

    });

  }
}
 

XML

 < RadioGroup中
        机器人:ID =@ + ID / radioSex
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT>

        <单选按钮
            机器人:ID =@ + ID / radioMale
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:文本=@字符串/ radio_male
            机器人:检查=真/>

        <单选按钮
            机器人:ID =@ + ID / radioFemale
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:文本=@字符串/ radio_female/>

    < / RadioGroup中>
 

I have a piece of code with three radiobuttons within a radiogroup. I want to set an oncheckedlistener that will show the value of the radiobutton in a Toast however what I have gotten so far is not working. How do I get the value of the radiobutton and display it in a Toast? Thank you in advance, this is my code:

package com.example.toohelps;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;

public class MainActivity extends Activity {

RadioGroup rg;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    RadioGroup rg = (RadioGroup) findViewById(R.id.radioGroup1);
    final String value = ((RadioButton)findViewById(rg.getCheckedRadioButtonId() )).getText().toString();

    rg.setOnCheckedChangeListener(new OnCheckedChangeListener() 
    {
        public void onCheckedChanged(RadioGroup group, int checkedId) {

            Toast.makeText(getBaseContext(), value, Toast.LENGTH_SHORT).show();
        }
    });

}

 }

XML file

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<RadioGroup
    android:id="@+id/radioGroup1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="152dp" >

    <RadioButton
        android:id="@+id/radio0"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:text="Choose 1" />

    <RadioButton
        android:id="@+id/radio1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:text="Choose 2" />

    <RadioButton
        android:id="@+id/radio2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Choose 3" />
 </RadioGroup>

 </RelativeLayout>

解决方案

Tested and working. Check this

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class MyAndroidAppActivity extends Activity {

  private RadioGroup radioSexGroup;
  private RadioButton radioSexButton;
  private Button btnDisplay;

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

    addListenerOnButton();

  }

  public void addListenerOnButton() {

    radioSexGroup = (RadioGroup) findViewById(R.id.radioSex);
    btnDisplay = (Button) findViewById(R.id.btnDisplay);

    btnDisplay.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

                // get selected radio button from radioGroup
            int selectedId = radioSexGroup.getCheckedRadioButtonId();

            // find the radiobutton by returned id
                radioSexButton = (RadioButton) findViewById(selectedId);

            Toast.makeText(MyAndroidAppActivity.this,
                radioSexButton.getText(), Toast.LENGTH_SHORT).show();

        }

    });

  }
}

xml

<RadioGroup
        android:id="@+id/radioSex"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <RadioButton
            android:id="@+id/radioMale"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/radio_male" 
            android:checked="true" />

        <RadioButton
            android:id="@+id/radioFemale"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/radio_female" />

    </RadioGroup>