android inputType=phone not working

一个人想着一个人 提交于 2020-01-04 14:18:32

问题


I am trying to get the inputType=phone to work consistently. It works perfectly in one project, but I can't get it to work in any other project. By "work", I mean that the number is not formatted as the value is typed. Here's a test project that does not work.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.jdot.testphoneinput2.MainActivity">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="phone"
        android:ems="10"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:id="@+id/editText"
        android:hint="enter phone" />
</RelativeLayout>

Here's the class declaration:

package com.jdot.testphoneinput2;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

Notes:

  1. The project where it works also inherits from AppCompatActivity
  2. I've created this same project with and without AppCompatActivity
  3. I've compared the manifests and gradles of the projects that do and don't work. There are no material differences other than the Activity definitions. Specifically, the versions are all the same.
  4. I've changed the inputType to "password" and it worked fine, so the problem appears to be related specifically to the phone type
  5. In the project that works, like the ones that don't, the phone field is the only EditText on the Activity page. When the Activity is loaded, the EditText gets focus. However, the soft keyword is only displayed automatically by the project that works. For the other projects, I have to tap the field in order to display the keyboard.
  6. In all projects, the root element in the XML is RelativeLayout
  7. I'm debugging on a Samsung S7 Edge

回答1:


You are probably forgetting something like this in your activity:

editText.addTextChangedListener(new PhoneNumberFormattingTextWatcher());

android:inputType="phone" doesn't format your input, it simply limits the set of symbols that can be entered.



来源:https://stackoverflow.com/questions/41146941/android-inputtype-phone-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!