简体   繁体   English

Android Studio - 如何在 Java 中以编程方式为 TextInputLayout 设置文本?

[英]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.

相关问题 以编程方式在 Android Studio 中将文本颜色设置为 textColorPrimary? - Set Text Color to textColorPrimary in Android Studio programmatically? Android:以编程方式添加 TextInputLayout - Android: Programmatically adding TextInputLayout 如何在 Android Material TextInputLayout 中将 SpannableString 设置为 EditText? - How to set a SpannableString to an EditText in an Android Material TextInputLayout? 如何在 Android 中为 TextInputLayout 设置默认值 - How to set default value for TextInputLayout in Android 如何在 Android Studio 中将文本设置为对应的图像 - How to set text to corresponding images in Android Studio 如何使用 java 以编程方式添加数字选择器( android 工作室) - How can I add a number picker programmatically with java ( android studio ) 为什么 TextInputEditText 在 Android Studio 中无法转换为 TextInputLayout? - Why TextInputEditText cannot be cast to TextInputLayout in Android Studio? 如何通过Java处理TextInputLayout - How to handle TextInputLayout through Java TextInputLayout - 动态设置浮动文本颜色 - TextInputLayout - set floating text color dynamically 如何在 Android Studio 中使用 Java 更改文本颜色和文本大小? - How to change Text Color and Text Size with Java in Android Studio?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM