获取线性布局

时间:2016-09-11 17:47:07

标签: android android-layout

我有一个linearlayout,我有动态创建的子视图,其中包含edittext,checkbox,spinner,单选按钮。我正在获得所有edittext的价值,但我不知道如何获得所有已选中复选框的值以及选中的单选按钮和微调器项的动态,请帮助我,下面是我的代码,以获取linearlayout的所有子视图。

      StringBuilder stringBuilder = new StringBuilder();
                for (int i = 0; i < ll.getChildCount(); i++) {
                    View newView = ll.getChildAt(i);
                    if (newView instanceof EditText) {
                        EditText et = (EditText) newView;
                        //validate your EditText here
                        stringBuilder.append("EditText: " + et.getText());
                        stringBuilder.append("/n");
                    } else if (newView instanceof RadioButton) {
                        RadioGroup radioGroup = (RadioGroup) newView; 

//need to get value of selected radtio button
                    } 
                    else if (newView instanceof Spinner) {
                        Spinner spinner = (Spinner) newView;
                        String str = spinner.getSelectedItem().toString();
                        stringBuilder.append("Spinner: " + str.toString());
                        stringBuilder.append("/n")

//here i am able to get value of spinner.getSelectedItem().toString(); but unable to append in stringBuilder.

                    } else if (newView instanceof CheckBox) {
                        CheckBox chk = (CheckBox) newView;

//need to get value of all selected checkbox 

                      }
                    }
                }

1 个答案:

答案 0 :(得分:0)

尝试这个以获取所选RadioButton的值:

radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId)
    {
        radioButton = (RadioButton) findViewById(checkedId);
        Toast.makeText(getBaseContext(), radioButton.getText(), Toast.LENGTH_SHORT).show();
    }
});