【问题标题】:Why does PhoneNumberFormattingTextWatcher which i applied to an EditText not format made of concurrent 1's为什么我应用于 EditText 的 PhoneNumberFormattingTextWatcher 不是由并发 1 组成的格式
【发布时间】:2019-04-19 21:36:58
【问题描述】:

我在我的 editText 字段上使用 PhoneNumberFormattingTextWatcher 我注意到它不会格式化以 11 开头的数字,而是将它们视为普通字符串。为什么是这样。有没有办法解决这个问题?

这是 XML:

<android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="8">
            <!-- INPUT -->
            <EditText
                android:id="@+id/etMobile"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="@dimen/dp_size_8"
                android:layout_marginBottom="@dimen/dp_size_8"
                android:background="@null"
                android:ems="10"
                android:hint="@string/edt_mobile"
                android:inputType="phone"
                android:maxLength="14"
                android:paddingTop="@dimen/dp_size_2" />

        </android.support.design.widget.TextInputLayout>

这是java活动中的用法:

etMobile.addTextChangedListener(new PhoneNumberFormattingTextWatcher());

当前本地是

美国

预期的数字格式是

1 222-222-2222

1 333-333-3333

但是当一个“1”跟在最初的“1”之后时,我得到了

11111111111111

【问题讨论】:

  • 显示 EditText 的 XML 代码
  • 请显示代码、输入文本、当前输出文本、预期输出文本、您运行的 android 版本、系统中设置的语言/区域设置。请更新您的问题并回答所有个问题,以便我们弄清楚发生了什么。
  • @EugenPechanec 完成
  • @PankajKumar 完成

标签: android


【解决方案1】:

对于您给定的区域设置,PhoneNumberFormattingTextWatcher 可能无法识别以 11 开头的数字。就我而言,它是任何以 7、6 或 2 开头的数字。

如果您想解决问题,您可以随时制作自己的文本观察器或使用 PhoneNumberUtils 的格式数字功能。

【讨论】:

  • 我实际上正在编写生产代码,所以我不习惯 PhoneNumberFormattingTextWatcher。但是语言环境是如何设置的呢?
  • 您可以在上面的 LOLLIPOP 设备上通过 new PhoneNumberFormattingTextWatcher(locale) 设置它,其中 local 是 ISO 3166-1 两个字母的国家/地区代码,例如“CA”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-10
  • 2021-10-22
  • 2015-08-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多