Android TextInputEditText.setText不接受formatArgs

穆罕默德·雷扎·哈哈尼(Mohammad Reza Khahani)

我使用Material TextInputEditText而不是EditText。但是TextInputEditText没有setText(resId:int,varargs formatArgs:Any!),当通过values-> strings.xml和EditText格式化字符串时非常有用。

所以我添加了这种扩展方法来做到这一点:

fun TextInputEditText.setText(@StringRes resId:Int, vararg formatArgs:Any ){
   val formattedText = resources.getString(resId, *formatArgs)
   this.setText(formattedText)
}

现在我可以这样使用它:

在values-> strings.xml中:

<string name="persian_date_format">%1$d/%2$d/%3$d</string>

在布局文件中:

<com.google.android.material.textfield.TextInputLayout
   style="@style/Widget.MaterialComponents.TextInputLayout.FilledBox"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:layout_marginStart="@dimen/d8"
   android:layout_marginTop="@dimen/d4"
   android:layout_marginEnd="@dimen/d8"
   android:hint="@string/hint_text"
   app:boxBackgroundColor="#fff"
   app:endIconMode="clear_text">

      <com.google.android.material.textfield.TextInputEditText
          android:id="@+id/pick_date"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:imeOptions="actionNext"
          android:maxLines="1"
          android:singleLine="true"
          android:text="@={viewModel.pickDate}" />
​
  </com.google.android.material.textfield.TextInputLayout>

在片段中,我可以使用这种扩展方法,如下所示:

binding.pickDate.setText(R.string.persian_date_format, year, monthOfYear, dayOfMonth)

有更好的解决方案吗?

阿萨杜扎曼医师

您可以直接使用String.format而不是创建extension函数。检查以下内容:

binding.pickDate.setText(String.format(R.string.persian_date_format, year, monthOfYear, dayOfMonth)) 

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

当语言环境为印地语时,TextInputEditText不接受电话号码

来自分类Dev

Android清单不接受更改

来自分类Dev

在 TextInputEditText 上设置 android:lines 不起作用

来自分类Dev

VideoBitmapDecoder 不接受 int (Android-Glide)

来自分类Dev

Android Material Components 主题 - 聚焦字段时更改 TextInputLayout/TextInputEditText 标签和下划线的颜色

来自分类Dev

为什么Android Studio不接受新的Java JDK位置

来自分类Dev

“ inputType =“ numberDecimal”不接受逗号,即使使用android:digits =“ 0123456789。,”

来自分类Dev

Android Volley库不接受getParam()中url中的参数

来自分类Dev

问:Android在Toast / Dialog上不接受特殊字符。

来自分类Dev

android编辑文本不接受数字输入

来自分类Dev

android OS在顺序更改上不接受横向模式设计

来自分类Dev

<button>标签在Android中不接受Html.fromHtml

来自分类Dev

Android asynctask将不接受上下文

来自分类Dev

Android Build 工具 26.0.2 不接受许可证

来自分类Dev

Android webview 不接受 .co 或 .org 扩展名

来自分类Dev

Keystore 问题 ---- Android (google play store) ----不接受证书

来自分类Dev

TextInputEditText调整文本大小

来自分类Dev

TextInputLayout正在覆盖TextInputEditText

来自分类Dev

Instagram为Android重定向Uri方案?不接受iOS方案。

来自分类Dev

Android Gradle记录器不接受java.lang.String

来自分类Dev

为什么Eclipse在nextFocusForward中不接受有效的Android ID?

来自分类Dev

Android Paypal SDK错误:商家不接受此类型的付款

来自分类Dev

Android Lollipop软键盘不接受带有GL表面的按键

来自分类Dev

处于休眠模式时,Android中的ServerSocket不接受[屏幕关闭]

来自分类Dev

Android Paypal SDK错误:商家不接受此类型的付款

来自分类Dev

android - Spinner setOnItemClickListener 不接受实现 OnItemClickListener 作为参数的命名类

来自分类Dev

Android firebase robo testlab gcloud 客户端不接受登录参数

来自分类Dev

更改TextInputEditText底行的空格

来自分类Dev

接受还是不接受

Related 相关文章

  1. 1

    当语言环境为印地语时,TextInputEditText不接受电话号码

  2. 2

    Android清单不接受更改

  3. 3

    在 TextInputEditText 上设置 android:lines 不起作用

  4. 4

    VideoBitmapDecoder 不接受 int (Android-Glide)

  5. 5

    Android Material Components 主题 - 聚焦字段时更改 TextInputLayout/TextInputEditText 标签和下划线的颜色

  6. 6

    为什么Android Studio不接受新的Java JDK位置

  7. 7

    “ inputType =“ numberDecimal”不接受逗号,即使使用android:digits =“ 0123456789。,”

  8. 8

    Android Volley库不接受getParam()中url中的参数

  9. 9

    问:Android在Toast / Dialog上不接受特殊字符。

  10. 10

    android编辑文本不接受数字输入

  11. 11

    android OS在顺序更改上不接受横向模式设计

  12. 12

    <button>标签在Android中不接受Html.fromHtml

  13. 13

    Android asynctask将不接受上下文

  14. 14

    Android Build 工具 26.0.2 不接受许可证

  15. 15

    Android webview 不接受 .co 或 .org 扩展名

  16. 16

    Keystore 问题 ---- Android (google play store) ----不接受证书

  17. 17

    TextInputEditText调整文本大小

  18. 18

    TextInputLayout正在覆盖TextInputEditText

  19. 19

    Instagram为Android重定向Uri方案?不接受iOS方案。

  20. 20

    Android Gradle记录器不接受java.lang.String

  21. 21

    为什么Eclipse在nextFocusForward中不接受有效的Android ID?

  22. 22

    Android Paypal SDK错误:商家不接受此类型的付款

  23. 23

    Android Lollipop软键盘不接受带有GL表面的按键

  24. 24

    处于休眠模式时,Android中的ServerSocket不接受[屏幕关闭]

  25. 25

    Android Paypal SDK错误:商家不接受此类型的付款

  26. 26

    android - Spinner setOnItemClickListener 不接受实现 OnItemClickListener 作为参数的命名类

  27. 27

    Android firebase robo testlab gcloud 客户端不接受登录参数

  28. 28

    更改TextInputEditText底行的空格

  29. 29

    接受还是不接受

热门标签

归档