动态添加编辑文本并获取其引用

时间:2015-11-09 23:08:00

标签: java android mobile-application

我希望能够在运行时添加“编辑文本”,并通过单击某个按钮来显示其引用。实现它的最佳方式是什么?

1 个答案:

答案 0 :(得分:0)

弄清楚这一点:

private void buildEditText() {
    LinearLayout layout = new LinearLayout(getActivity());
    LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    layout.setLayoutParams(layoutParams);
    layout.setOrientation(LinearLayout.VERTICAL);

    EditText editText = (EditText) inflater.inflate(R.layout.your_custom_layout, null); // Here is your reference
    LayoutParams textViewParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    editText .setLayoutParams(textViewParams);
    layout.addView(textView);
    this.editText = editText ;
    this.editText .setImeOptions(EditorInfo.IME_ACTION_DONE);
    editText .setText("If you have some text to enter");

    editText .setHint("Some hint");
    editText .setInputType(EditorInfo.TYPE_TEXT_FLAG_CAP_CHARACTERS); 

    parentView.addView(layout);
}