... 所以我的问题是如何从这些动态元素中获取值并执行一些计算以在textview上显示结果,textview也是动态创建的。 先谢谢你们!!
public void goButtonClicked(View view) {
maalContainer.removeAllViews();
int numberofPlayersTolayout = (Integer) Integer.parseInt((String) numberOfPlayers.getSelectedItem());
LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
for (int i = 0; i < numberofPlayersTolayout; i++) {
View dynamicEntryView = inflater.inflate(R.layout.player_entry_item, null);
maalContainer.addView(dynamicEntryView, params);
}
}
}
答案 0 :(得分:5)
for (int i = 0; i < maalContainer.getChildCount(); i++) {
View view = maalContainer.getChildAt(i);
EditText ed_item = (EditText) view
.findViewById(R.id.edittext1);
EditText ed_value = (EditText) view
.findViewById(R.id.edittext2);
EditText ed_value1 = (EditText) view
.findViewById(R.id.edittext3);
ed_item.getText().toString().trim();
ed_value.getText().toString().trim();
ed_value1.getText().toString().trim();
}
答案 1 :(得分:2)
无需分配/获取任何视图的ID,但最好的方法是遍历LinearLayout
的子视图:
int childcount = myLinearLayout.getChildCount();
for (int i=0; i < childcount; i++){
View v = myLinearLayout.getChildAt(i);
// do whatever you would want to do with this View
}
答案 2 :(得分:2)
这里有一些事情:
您不应该使用Application
Context
来获取LayoutInflater
。你应该从Activity
Context
获得,否则你的主题和风格将不受尊重。
您不应该使用null作为第二个参数进行充气(除非在您不知道View
的父级的特殊情况下)。只需将android:layout_width="match_parent"
和android:layout_height="wrap_content"
属性放在player_entry_item.xml
中,通货膨胀就会受到尊重。
将EditText
个对象存放在通货膨胀时作为私人阵列。
话虽如此,我建议修改:
private EditText[] mEditTextPlayers;
public void goButtonClicked(View view) {
maalContainer.removeAllViews();
int numPlayers = Integer.parseInt((String) numberOfPlayers.getSelectedItem());
LayoutInflater inflater = LayoutInflater.from(view.getContext());
mEditTextPlayers = new EditText[numPlayers];
for (int i = 0; i < numPlayers; i++) {
//Pass the parent as the second parameter to retain layout attributes
mEditTextPlayers[i] = inflater.inflate(R.layout.player_entry_item, maalContainer, false);
maalContainer.addView(dynamicEntryView);
}
}
答案 3 :(得分:2)
public void goButtonClicked(View view) {
int numberofPlayersTolayout = Integer.parseInt(numberOfPlayers.getSelectedItem().toString());
LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
maalContainer.removeAllViews();
maalText = new EditText[numberofPlayersTolayout];
points = new EditText[numberofPlayersTolayout];
result = new TextView[numberofPlayersTolayout];
for (int i = 0; i < numberofPlayersTolayout; i++) {
View dynamicEntryView = inflater.inflate(R.layout.player_entry_item, null);
maalContainer.addView(dynamicEntryView, params);
TextView playerName = (TextView) dynamicEntryView.findViewById(R.id.player_name_textview);
playerName.setText("Player :" + (i + 1));
maalText[i] = (EditText) dynamicEntryView.findViewById(R.id.player_item_edittext_maal);
points[i] = (EditText) dynamicEntryView.findViewById(R.id.player_item_edittext_point);
result[i] = (TextView) dynamicEntryView.findViewById(R.id.player_item_textview_result);
}
答案 4 :(得分:1)
您可以使用视图的getId()
和setId()
方法来完成此操作。
dynamicEntryView.setId(i);
并通过获取
访问以下视图 dynamicEntryView.getId(i);
答案 5 :(得分:1)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/player_name_textview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10sp"
android:layout_marginTop="10sp"
android:text="Player name"
android:textColor="#202020"
android:textSize="18sp" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<EditText
android:id="@+id/player_item_edittext_point"
android:layout_width="0sp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="Points"
android:inputType="number"
android:textColor="#202020" />
<EditText
android:id="@+id/player_item_edittext_maal"
android:layout_width="0sp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="Maal"
android:inputType="number"
android:textColor="#202020" />
<TextView
android:id="@+id/player_item_textview_result"
android:layout_width="0sp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="0.00$"
android:textSize="22sp"
android:typeface="serif" />
</LinearLayout>
</LinearLayout>
仅使用一个ID。它将根据玩家数量循环。希望它会对你有所帮助。 @ user2731584