【问题标题】:Add EditText by button通过按钮添加 EditText
【发布时间】:2013-09-04 13:33:45
【问题描述】:

如何通过Button 中的onClick 方法动态添加新的EditText?我已经有一个EditText,但我想在相同的布局上添加其他EditText。另外,EditText 必须与 counter++ 同名

【问题讨论】:

  • the EditText must have the same name with counter???可能是同一个ID?

标签: android android-layout android-edittext android-button buttonclick


【解决方案1】:

您必须使用 LayoutInflater。这是 Android SDK 做这些事情的方式......

请参阅此完整教程以了解如何使用 LayoutInflater:http://myandroidsolutions.blogspot.com.br/2012/06/android-layoutinflater-turorial.html

【讨论】:

    【解决方案2】:
    EditText edText = new EditText(this);
                edText .setId("");
                edText .setLayoutParams(new LinearLayout.LayoutParams(
                        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,
                        1f));
    

    【讨论】:

      【解决方案3】:
      @Override
      public void onClick(View v) {
          EditText edtNew = new EditText(this);
          edtNew.setId("edtNew"+"parentLayoutName".getChildCount()+1); // "edtNew" is prefix name EditText id , "parentLayoutName" Where you add EitText
          edtNew.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT));
          "parentLayoutName".addView(edtNew);
      }
      

      【讨论】:

        猜你喜欢
        • 2015-07-02
        • 1970-01-01
        • 2012-05-29
        • 2015-05-11
        • 2017-12-02
        • 1970-01-01
        • 2021-07-02
        • 1970-01-01
        • 2021-08-28
        相关资源
        最近更新 更多