简体   繁体   English

如何在 Android 中为 TextInputLayout 设置默认值

[英]How to set default value for TextInputLayout in Android

I'm using the TextInputLayout from Google's Material Design on Android.我在 Android 上使用Google Material Design中的TextInputLayout I want to set the default text value for the text field.我想为文本字段设置默认文本值。

I know I can set the hint, helper text, and error message, but I want to programatically set the default text value that in other cases would eventually be input by the user.我知道我可以设置提示、帮助文本和错误消息,但我想以编程方式设置在其他情况下最终将由用户输入的默认文本值。

According to the docs you can technically switch the inner TextInputEditText to a regular EditText but Google does not recommend doing this.根据文档,您可以在技术上将内部TextInputEditText切换为常规EditText ,但 Google 不建议这样做。 Should I explore the risks of doing that or is there a built-in solution I'm missing?我应该探索这样做的风险还是我缺少内置的解决方案?

You can use setText method for TextInputEditTest as below where ever you want:您可以在任何地方使用setText方法进行TextInputEditTest如下所示:

textInputEditText.setText(R.string.yourStringName);//string from resources
textInputEditText.setText("yourString");//static string

If you want to set default text when your activity or fragment initialize You should use setText in onCreate method which is the first callback in activity and fragment life cycle.如果您想在活动片段初始化时设置默认文本,您应该在onCreate方法中使用 setText,这是活动和片段生命周期中的第一个回调。

And the last thing: you can set default text for TextInputEditText not TextInputLayout because TextInputLayout extends from LinearLayout and therefore it doesn't have text property.最后一件事:您可以为TextInputEditText而不是TextInputLayout设置默认文本,因为 TextInputLayout 从 LinearLayout 扩展,因此它没有 text 属性。

Not really sure what your use case is, but you may think of one of the following:不太确定您的用例是什么,但您可能会想到以下其中一种:

Case 1: You want the user to see the default text案例 1:您希望用户看到默认文本

  1. Write your default text in the hint field.在提示字段中写下您的默认文本。
  2. When it's time for the app to collect the data, if the View has no text, then use the default text.当应用程序收集数据时,如果 View 没有文本,则使用默认文本。

Case 2: User doesn't need to see the default text案例 2:用户不需要看到默认文本

  1. Keep your hint text as you have it now.保持您现在拥有的提示文本。
  2. When it's time for the app to collect the data, if the View has no text, then use the default text.当应用程序收集数据时,如果 View 没有文本,则使用默认文本。

If either of these cases don't work for you, please clarify the user experience you are seeking.如果这两种情况都不适合您,请说明您正在寻求的用户体验。 Eg, do you want to wait after a certain event/time to switch the hint with a default text?例如,您是否想在某个事件/时间后等待以使用默认文本切换提示?

<com.google.android.material.textfield.TextInputLayout
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:hint="@string/form_username">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Android Material TextInputLayout 中将 SpannableString 设置为 EditText? - How to set a SpannableString to an EditText in an Android Material TextInputLayout? Android Studio - 如何在 Java 中以编程方式为 TextInputLayout 设置文本? - Android Studio - How to set text for TextInputLayout programmatically in Java? Android:如何为参数变量设置默认值 - Android: How to set a default value for an argument variable 如何在 Android 中为 SwitchPreference 设置默认值? - How to set default value for SwitchPreference in Android? Android如何从SharedPreference设置EditTextPreference的默认值? - Android how to set default value of EditTextPreference from SharedPreference? 如何使用Kotlin在ViewModel中使用TextInputLayout设置错误标签 - How to set error label with TextInputLayout in the viewmodel with kotlin 如何为材料设计 TextInputLayout 添加值(setText)? - How to add value (setText) to a material design TextInputLayout? 如何在Android中设置默认布局 - How To Set Default Layout in Android 如何为@JsProperty设置默认值? - How to set a default value for a @JsProperty? 如何设置jSpinner的默认值? - how to set jSpinner default value?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM