无法在android中动态创建的编辑文本字段中输入文本

时间:2014-11-17 11:19:11

标签: java android android-edittext

EditText lEditText;
LinearLayout mLinearLayout = new LinearLayout(getApplicationContext());
mLinearLayout = (LinearLayout)findViewById(R.id.mylinearlayout);
lEditText = new EditText(getApplicationContext());
                lEditText .setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));

                 lEditText.setId(8);
                 lEditText.setHint("Text View");
                 lEditText.setEms(10);
                 lEditText.setInputType(InputType.TYPE_CLASS_TEXT);
                 lEditText.setWidth(200);     // change width
                 lEditText.setHeight(20);   //chng hight
                 lEditText.setPadding(50, 20, 20, 500);
                 mLinearLayout.addView(lEditText);

我已经为此活动制作了XML。这段代码显示我只是编辑文本,但我无法在此输入输入。

1 个答案:

答案 0 :(得分:0)

试试这个。

LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);

LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);

EditTexttv = new EditText(this);
tv.setText("This is a Edittext. Please input here");
tv.setLayoutParams(params);

layout.addView(tv);

LinearLayout.LayoutParams layoutParam =
new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT );

this.addContentView(layout, layoutParam);