如何在java中以编程方式为textinputlayout设置文本?

z9smfwbn  于 2021-08-20  发布在  Java
关注(0)|答案(2)|浏览(237)

要为edittext之类的视图设置文本,我只需要使用 setText("text") 函数,但我无法对textinputlayout执行此操作。如何设置textinputlayout的文本?

brjng4g3

brjng4g31#

只需将设置为 TexInputEditText .

<com.google.android.material.textfield.TextInputLayout
        android:id="@+id/textInputLayout"
        ..>
        <com.google.android.material.textfield.TextInputEditText/>
    </com.google.android.material.textfield.TextInputLayout>

比如:

textInputLayout.getEditText().setText("text");
qnakjoqk

qnakjoqk2#

要在textinputlayout中设置文本,需要在其中创建edittext。
比如:

TextInputLayout parent = (TextInputLayout) findViewById(R.id.parent);
        EditText editText = new EditText;
        editText.setText("the text");
        editText.setLayoutParams(new TextInputLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        parent.addView(editText);
        }

相关问题