[英]Android Studio - How to set text for TextInputLayout programmatically in Java?
To set text for views like EditText, I simply need to use setText("text")
function, but I can't do that to TextInputLayout.要为 EditText 等视图设置文本,我只需要使用
setText("text")
函数,但我不能对 TextInputLayout 这样做。 How do I set text for TextInputLayout?如何为 TextInputLayout 设置文本?
Just have to set the the to the TexInputEditText
.只需将 设置为
TexInputEditText
。
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/textInputLayout"
..>
<com.google.android.material.textfield.TextInputEditText/>
</com.google.android.material.textfield.TextInputLayout>
Something like:就像是:
textInputLayout.getEditText().setText("text");
To set text in TextInputLayout you'd need to create an EditText within it.要在 TextInputLayout 中设置文本,您需要在其中创建一个 EditText。
Something like:就像是:
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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.