How to get the text from radio button in a radio group when radio button checked state is changed(更改单选按钮选中状态时如何从单选按钮组中的单选按钮获取文本)
问题描述
我在一个单选组中动态创建了两个单选按钮,其中一个被选中.当我检查另一个按钮时,我需要它的值应该保存在字符串中.但是我已经为此实现了 checkedchangelistener
.但它第一次不起作用.这是我的代码.
I have created two radio buttons in a radio group dynamically and one of them is checked.
I need when i cheked another button then its value should be saved in string.
But i have implemented checkedchangelistener
for this.But its not working first time.
Here is my code.
rg = ((RadioGroup)getActivity().findViewById(alist_id.get(i)));
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){
public void onCheckedChanged(RadioGroup rd, int checkedId) {
for(int i=0; i<rd.getChildCount(); i++) {
radio_button = (RadioButton) rd.getChildAt(i);
int id = radio_button.getId();
if(radio_button.getId() == checkedId) {
text = radio_button.getText().toString();
flag=true;
System.out.println("trueeeeeeeee"+text);
return;
}
}
}
});
if (flag==true) {
updated_list.add(text);
System.out.println("sssssssssssssssssss");
}else {
updated_list.add(data_from_list_view.get(i));
System.out.println("falseeeeeee");
}
推荐答案
xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<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>
<Button
android:id="@+id/btnDisplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn_display" />
</LinearLayout>
java代码
package com.mkyong.android;
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();
}
});
}
}
这篇关于更改单选按钮选中状态时如何从单选按钮组中的单选按钮获取文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:更改单选按钮选中状态时如何从单选按钮组中的单选按钮获取文本
基础教程推荐
- 降序排序:Java Map 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01