单击按钮添加EditText

时间:2015-04-23 19:32:07

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

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);


    }

1 个答案:

答案 0 :(得分:0)

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