【问题标题】:PhoneNumberFormattingTextWatcher is not workingPhoneNumberFormattingTextWatcher 不起作用
【发布时间】:2013-01-27 03:01:40
【问题描述】:

我正在尝试使用 PhoneNumberFormattingTextWatcher,但没有任何反应,好像代码不存在一样

这里是代码

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
    EditText PhoneEdit = (EditText) findViewById(R.id.editText1); 
    PhoneEdit.addTextChangedListener(new PhoneNumberFormattingTextWatcher());
}

输入 002010555666 还是一样的 no - or + or (),一样没有任何格式 代码中是否缺少任何东西

帮助

【问题讨论】:

  • 您在哪个设备上试用?

标签: android formatting android-edittext textwatcher


【解决方案1】:

最终解决方案:该问题仅与您在 XML 中选择的 android:inputType 相关。它不适用于“数字”。您需要选择“电话”或无输入类型。

【讨论】:

    【解决方案2】:

    即使在 XML 中添加 android:inputType="phone" 后我也遇到了问题。

    解决方案是在手机中设置语言

    设置 -> 搜索语言 -> 选择英语(美国)

    格式 (999) 999-9999 主要用于美国,因此电话语言应仅设置为该格式。

    【讨论】:

    • 您还可以在 PhoneNumberFormattingTextWatcher 构造函数中使用国家代码,例如new PhoneNumberFormattingTextWatcher("US"),如果您只支持特定国家/地区的电话号码。
    【解决方案3】:

    我对 YTerle 的回答添加了评论,但我想将完整的答案放在下面:

    <android.support.design.widget.TextInputEditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:inputType="phone"
                    android:hint="@string/phone_number"
                    android:layout_marginBottom="5dp"
                    android:ems="10"
                    android:maxLength="14"
                    android:id="@+id/phone" />
    

    然后在代码中输入以下内容:

    phoneView.addTextChangedListener(new PhoneNumberFormattingTextWatcher());
    

    【讨论】:

    • 如果您将android:digits="0123456789" 设置为 EditText 以仅允许某些数字/字符,它将不起作用。
    【解决方案4】:

    遇到了同样的问题,但删除 android:digits="1234567890+" 后它就消失了。

    【讨论】:

    • 这是我的问题 - 我必须有电话的输入类型,然后我也删除了数字。
    • 也遇到了这个问题,但我没有将其删除,而是将其修改为android:digits="1234567890+()-"
    • @DanielNugent 我建议也添加一个空格,因为一些国际格式使用它:android:digits="1234567890 +()-"
    • 为什么 'android:digits' 会造成问题?我应该如何解决这个问题?
    • @shaithana 和 DanielNugent 非常感谢大家。我花了一个小时寻找这个解决方案。
    【解决方案5】:

    我在应用程序中使用过它,它在模拟器中运行良好(Android_4.2_Emulator_Smartphone_Nexus_S 使用平台 4.4.2,API 19)但在运行平台 4.1.2 的三星 Tab 3 设备上运行时完全没有效果, API 17. 也许它依赖于设备?文档表明它是在 API 1 中引入的,所以我认为它现在应该已经解决了所有问题,但显然没有。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-16
      • 1970-01-01
      • 2016-03-13
      • 1970-01-01
      • 1970-01-01
      • 2016-09-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多