2016-09-11 54 views
0

我有一個線性佈局,我有動態創建的子視圖,其中包含edittext,複選框,微調框,單選按鈕。我得到的所有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 

         } 
        } 
       } 
+0

'chk.isChecked()' – skywall

+0

當你創建它們,您可以存儲的意見引用。那麼你根本不需要這個代碼。 –

回答

0

嘗試一下本作選擇的單選按鈕獲取價值:

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(); 
    } 
}); 
相關問題