【问题标题】:How to get data from dynamically created EditText using LayoutInflater in android?如何在android中使用LayoutInflater从动态创建的EditText中获取数据?
【发布时间】:2014-01-22 05:01:42
【问题描述】:

这是像这样动态创建的相对布局视图:

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

}

}

【问题讨论】:

标签: android android-edittext android-relativelayout layout-inflater


【解决方案1】:
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();
}

【讨论】:

    【解决方案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
    }
    

    【讨论】:

    • 我的 LinearLayout 由 EditText1,2,3 组成,例如,线性布局扩展到 LinearLayout1..10,所以我想遍历所有 EditText 并获取数据。谢谢..
    【解决方案3】:

    这里有几件事:

    1. 您不应该使用Application Context 来获取LayoutInflater。你应该从ActivityContext 那里得到它,否则你的主题和风格将不会得到尊重。

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

    3. 在膨胀时将 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);
        }
    }
    

    【讨论】:

    • 那是mEditTextPlayers。它不是本地引用,而是包含您膨胀的所有 EditText 对象的类引用。然后您可以通过它们的索引访问它们中的每一个(例如mEditTextPlayers[0].getText().toString()
    • 我的 player1 到第 n 个玩家必须显示 EditText1 EditText2 和 TextView1 但您的 mEditTextPlayers 仅定义为 EditText。 #kcoppock 谢谢。
    • 然后只需制作一个数组数组或其他一些映射。 EditText 扩展了 TextView 所以它实际上甚至可以只是一个 TextViews mod 3 的数组。
    • #kcoppock 即使我使用了三个edittext 然后上面的代码也没有工作......它未能给出错误说方法无法执行......感谢您的帮助......
    【解决方案4】:
         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);
    }
    

    【讨论】:

    • 我知道已经很晚了,但是你能分享一下 XML 文件吗?我想知道的是——在 EDitText 和 TextView 的 XML 中,您是否提到过“android:id”字段?如果是这样,行中的所有 EditText 都将具有相同的 id - 对吗?我有点困惑。请帮忙。
    • 我在上面的字段中添加了xml。 @user2731584
    • 感谢您回复我。我想通了!
    【解决方案5】:

    您可以使用 View 的 getId()setId() 方法来做到这一点。

       dynamicEntryView.setId(i);
    

    并通过获取

    来访问如下视图
       dynamicEntryView.getId(i);
    

    【讨论】:

      【解决方案6】:
      <?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

      【讨论】:

        猜你喜欢
        • 2011-08-20
        • 2021-01-18
        • 1970-01-01
        • 2018-05-26
        • 1970-01-01
        • 1970-01-01
        • 2015-05-23
        • 1970-01-01
        相关资源
        最近更新 更多