我正在創建一個動態數量的editTexts,並且最終需要爲每個ID調用EditText上的.getText()。無法以編程方式設置EditText ID
不過,我注意到,這是很難以編程方式設置ID,所以我使用的.setTag()方法:
private void createAnswerChoice(int answerNumber) {
ViewGroup layout = (ViewGroup) mRootView.findViewById(R.id.create_poll_questions_answer_layout);
EditText editText = new EditText(getActivity());
editText.setHint(getResources().getString(R.string.answer_text) + " " + answerNumber);
editText.setSingleLine(true);
editText.setImeOptions(EditorInfo.IME_ACTION_DONE);
String editTextID = ((getResources().getString(R.string.created_answer_editText_id))+String.valueOf(answerNumber));
editText.setTag(editTextID);
Toast.makeText(getActivity().getApplicationContext(), editTextID, Toast.LENGTH_SHORT).show();
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
editText.setLayoutParams(layoutParams);
TextInputLayout newAnswer = new TextInputLayout(getActivity());
newAnswer.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
newAnswer.addView(editText, layoutParams);
layout.addView(newAnswer);
}
我怎麼會得到每個EDITTEXT的值,如果我知道標記而不是ID?另外,.setTag()方法的目的是什麼(它與.setID()有什麼關係?)
什麼是設置ID的問題? – pratnala
如果您使用的是android,那麼建議使用xml而不是以編程方式創建視圖。它更容易進行更改並提供更清晰的代碼 – suku
我正在創建基於用戶選擇的數字的動態視圖,因此靜態XML(據我所知)將不夠用 – tccpg288