如何使用Android中的LayoutInflater从动态创建的EditText中获取数据?

时间:2014-01-22 05:01:42

标签: android android-edittext relativelayout layout-inflater

这是相对布局视图,它是动态创建的,如下所示:

Layout

... 所以我的问题是如何从这些动态元素中获取值并执行一些计算以在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);
    }

}

}

6 个答案:

答案 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)

这里有一些事情:

  1. 您不应该使用Application Context来获取LayoutInflater。你应该从Activity Context获得,否则你的主题和风格将不受尊重。

  2. 您不应该使用null作为第二个参数进行充气(除非在您不知道View的父级的特殊情况下)。只需将android:layout_width="match_parent"android:layout_height="wrap_content"属性放在player_entry_item.xml中,通货膨胀就会受到尊重。

  3. EditText个对象存放在通货膨胀时作为私人阵列。

  4. 话虽如此,我建议修改:

    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