【问题标题】:Adding EditText with button click通过单击按钮添加 EditText
【发布时间】:2015-07-02 04:27:24
【问题描述】:

EBelow 是我将 EditText 添加到我的布局的代码,它工作正常,但我希望它在创建的那些下方创建新的 EditText 框。如果您在代码中看到我应该更改或改进的内容,请发表评论。提前致谢。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my_weight_log);
    relativeLayout = (RelativeLayout)findViewById(R.id.relativeLayout);
    tt= (EditText)findViewById(R.id.tt);
    dd = (EditText)findViewById(R.id.dd);
    aa = (Button) findViewById(R.id.add);
    aa.setOnClickListener(new Button.OnClickListener()
    {public void onClick
                (View  v) { aa();}});

}


public void aa()
{         for(i = 0; i <100; ++i);

    RelativeLayout layout = (RelativeLayout)  findViewById(R.id.relativeLayout);
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams (
    RelativeLayout.LayoutParams.WRAP_CONTENT,
    RelativeLayout.LayoutParams.WRAP_CONTENT);
    params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
    params.addRule(RelativeLayout.BELOW);
    params.setMargins(0,600,0,0);

        EditText edtTxt = new EditText(this);
        int maxLength = 5;
        edtTxt.setHint("editText1");
        edtTxt.setLayoutParams(params);
        edtTxt.setBackgroundColor(Color.WHITE);
        edtTxt.setInputType(InputType.TYPE_CLASS_DATETIME);
        edtTxt.setTextSize(TypedValue.COMPLEX_UNIT_SP,18);
        InputFilter[] fArray = new InputFilter[1];
        fArray[0] = new InputFilter.LengthFilter(maxLength);
        edtTxt.setFilters(fArray);


    layout.addView(edtTxt);

    RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT);
            params1.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
            params1.setMargins(0,600,0,0);

    EditText editText = new EditText(this);
    int maxLength1 = 4;
    editText.setHint("editText2");
    editText.setLayoutParams(params1);
    editText.setBackgroundColor(Color.WHITE);
    editText.setInputType(InputType.TYPE_CLASS_NUMBER);
    editText.setTextSize(TypedValue.COMPLEX_UNIT_SP,18);
    fArray[0] = new InputFilter.LengthFilter(maxLength1);
    editText.setFilters(fArray);

    layout.addView(editText);


    }

【问题讨论】:

  • 在 onCreate 方法中检查 'aa'
  • 这是我复制代码时犯的一个错误,我现在进行了更正。我仍然无法在创建的编辑文本下方获取新的编辑文本。

标签: javascript android android-activity android-studio android-edittext


【解决方案1】:

尝试更改为线性布局并将其方向设置为垂直。然后,如果您要添加一个新视图,它将添加到您之前的视图下方。

【讨论】:

  • 我试过这个,但我希望 EditTexts 彼此交叉。我不知道如何在线性布局中做到这一点。
  • 实际上,我想出了如何使用您的 ans 和 Sachin 的示例来做到这一点。感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-05-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多