android - 为什么我无法获取添加的单选按钮或编辑文本的值?

标签 android layout android-edittext radio-group

我已经完成了一个应用程序,该应用程序如何将一些自定义布局动态添加到线性布局中。我的问题是当我想检索添加的此布局的某些组件的值时。

我有三种类型的 XML 可以动态添加。

一个

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="5dp"
    android:layout_margin="@dimen/tv_margin"
    android:padding="@dimen/padding_container"
    android:gravity="center">

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="$$$"
        android:id="@+id/tv_question"
        android:textColor="@color/ferro_palette_black"/>

    <RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/rg_answers"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="@dimen/ll_main_padding_top">

        <RadioButton
            android:id="@+id/rb_value_1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:button="@null"
            android:drawableBottom="?android:attr/listChoiceIndicatorSingle"
            android:gravity="center_horizontal|bottom"
            android:text="1"
            android:textColor="@color/ferro_palette_black" />

        <RadioButton
            android:id="@+id/rb_value_2"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:button="@null"
            android:drawableBottom="?android:attr/listChoiceIndicatorSingle"
            android:gravity="center_horizontal|bottom"
            android:text="2"
            android:textColor="@color/ferro_palette_black"/>

        <RadioButton
            android:id="@+id/rb_value_3"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:button="@null"
            android:drawableBottom="?android:attr/listChoiceIndicatorSingle"
            android:gravity="center_horizontal|bottom"
            android:text="3"
            android:textColor="@color/ferro_palette_black"/>

        <RadioButton
            android:id="@+id/rb_value_4"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:button="@null"
            android:drawableBottom="?android:attr/listChoiceIndicatorSingle"
            android:gravity="center_horizontal|bottom"
            android:text="4"
            android:textColor="@color/ferro_palette_black"/>

        <RadioButton
            android:id="@+id/rb_value_5"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:button="@null"
            android:drawableBottom="?android:attr/listChoiceIndicatorSingle"
            android:gravity="center_horizontal|bottom"
            android:text="5"
            android:textColor="@color/ferro_palette_black"/>

    </RadioGroup>
</LinearLayout>

两个

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="5dp"
    android:layout_margin="@dimen/tv_margin"
    android:padding="@dimen/padding_container"
    android:gravity="center">

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="$$$"
        android:id="@+id/tv_question"
        android:textColor="@color/ferro_palette_black"/>

    <RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/rg_answers"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="@dimen/ll_main_padding_top">

        <RadioButton
            android:id="@+id/rb_value_yes"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:button="@null"
            android:drawableBottom="?android:attr/listChoiceIndicatorSingle"
            android:gravity="center_horizontal|bottom"
            android:text="@string/yes"
            android:textColor="@color/ferro_palette_black"/>

        <RadioButton
            android:id="@+id/rb_value_no"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:button="@null"
            android:drawableBottom="?android:attr/listChoiceIndicatorSingle"
            android:gravity="center_horizontal|bottom"
            android:text="@string/no"
            android:textColor="@color/ferro_palette_black"/>


    </RadioGroup>
</LinearLayout>

三个

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="5dp"
    android:layout_margin="@dimen/tv_margin"
    android:padding="@dimen/padding_container"
    android:gravity="center">

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="$$$"
        android:id="@+id/tv_question"
        android:textColor="@color/ferro_palette_black"/>

    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="textMultiLine"
        android:ems="10"
        android:id="@+id/et_answer"
        android:textColor="@color/ferro_palette_black"/>

</LinearLayout>

我已经完成了下一个膨胀和获取值的方法。

public void addQuestion(LinearLayout mRootLayout, Context _c, int quiz_type, String question, int order, int answer_id)
    {
        LayoutInflater inflater = (LayoutInflater) _c.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
        int id_layout = -1;
        if(quiz_type == 1) {
            id_layout = R.layout.quiz_type_one;
        }else if(quiz_type == 2){
            id_layout = R.layout.quiz_type_two;
        }else if(quiz_type == 3){
            id_layout = R.layout.quiz_type_three;
        }

        View mChildView = inflater.inflate(id_layout, mRootLayout, false);
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        mRootLayout.addView(mChildView, params);

        TextView tv_question = (TextView) mChildView.findViewById(R.id.tv_question);
        tv_question.setText(question);

        if(quiz_type == 1) {
            RadioGroup rg_answers = (RadioGroup)mChildView.findViewById(R.id.rg_answers);
            int value = rg_answers.getCheckedRadioButtonId();
        }else if(quiz_type == 2){
            RadioGroup rg_answers = (RadioGroup)mChildView.findViewById(R.id.rg_answers);
            int value = rg_answers.getCheckedRadioButtonId();
        }else if(quiz_type == 3){
            EditText et_answer = (EditText)mChildView.findViewById(R.id.et_answer);
            String value = et_answer.getText().toString();
        }

    }

出于某种原因,每次我在变量值中得到 -1 或 ""。为什么??

感谢您的帮助。

最佳答案

通过查看您的代码,在此方法 addQuestion() 中,您正在使布局焕然一新,

因此在您 try catch 值的方法的下面部分中,当您尝试从新膨胀的 View 中读取值时,它将始终为 -1 和“”。

如果您想获取用户输入的值,请尝试实现 OnClick 监听器,或者也许按下您启动值读取的按钮。

关于android - 为什么我无法获取添加的单选按钮或编辑文本的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25763654/

相关文章:

android - Android应用中TypedArray的使用

android - 在 'mapview' 中显示当前位置时无法标记

java - JPanel 布局图像截断

python - PyQt5 相当于 WPF StackPanel

android - setCompoundDrawables EditText 文字重叠

android - 如何限制 EditText 使用表情符号

android - 我可以在缓存之前以某种方式从 OkHttp 响应中删除 header 吗?

android - visual studio 2013 cordova 混合应用程序插件 : Location of apk file

java - JLabel重叠

java - 无法从三个 EditText 获取结果?