小编bli*_*itz的帖子

在自定义警报对话框中获取单选按钮的值

我创建了一个带有单选组、单选按钮和编辑文本的自定义警报对话框。

目标是尝试从选定的单选按钮和编辑文本中获取文本值。此时,我只能获取edittext中输入的值。

我已经看到了下面的链接,并尝试调整代码以适应,但似乎代码仍然不起作用。

Android 从选定的单选按钮中获取价值

http://www.mkyong.com/android/android-radio-buttons-example/

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<RadioGroup
    android:id="@+id/radioPersonGroup"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <RadioButton
        android:id="@+id/gButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:layout_marginLeft="16dp"
        android:layout_weight="1"
        android:text="G" />
    <RadioButton
        android:id="@+id/kButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:layout_marginLeft="16dp"
        android:layout_weight="1"
        android:text="K" />

</RadioGroup>

<EditText
    android:id="@+id/editText2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="16dp"
    android:layout_marginLeft="16dp"
    android:layout_weight="1"
    android:ems="10"
    android:hint="$10.00"
    android:inputType="numberDecimal" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

java文件

private RadioButton radioSelectedButton;
...

FloatingActionButton fab = findViewById(R.id.fab);
final RadioGroup group = findViewById(R.id.radioPersonGroup);
fab.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        final …
Run Code Online (Sandbox Code Playgroud)

android android-alertdialog android-radiogroup android-radiobutton

3
推荐指数
1
解决办法
2357
查看次数