为什么我无法获得添加的radiobuttons或edittexts的值?

时间:2014-09-10 10:59:25

标签: android layout android-edittext radio-group

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

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

ONE     

<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>

TWO

<?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或“”。为什么?

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

通过查看您的代码,在此方法addQuestion()中,您正在为新的布局充气,

因此,在您尝试捕获值的方法的下方部分中,当您尝试从新近膨胀的视图中读取值时,它将始终为-1和“”。

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