2016-12-14 144 views
2

我想讓inputType = phone一致地工作。它在一個項目中完美地工作,但我無法讓它在任何其他項目中工作。通過「工作」,我的意思是該數字沒有被格式化,因爲該值是鍵入的。這是一個不起作用的測試項目。android inputType = phone not working

<?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> 

這裏的類聲明:

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); 
    } 
} 

注:

  1. 它的工作原理也AppCompatActivity繼承該項目
  2. 我創建了這個相同的項目,並沒有AppCompatActivity
  3. 我比較了項目的清單和階段這樣做和不工作。除了活動定義之外,沒有任何重大差異。具體來說,版本都是一樣的。
  4. 我已經將inputType更改爲「密碼」,並且它工作正常,所以問題似乎與手機型號特別相關
  5. 在有效的項目中,就像沒有的項目一樣,手機區域是活動頁面上唯一的EditText。當Activity被加載時,EditText獲得焦點。但是,軟關鍵字只能由有效的項目自動顯示。對於其他項目,我必須點擊該字段才能顯示鍵盤。
  6. 在所有項目中,在XML根元素是RelativeLayout的
  7. 我調試的一臺三星S7邊緣

回答

5

你可能會忘記在你的活動是這樣的:

editText.addTextChangedListener(new PhoneNumberFormattingTextWatcher()); 

android:inputType="phone"不會格式化您的輸入,它只是限制可以輸入的符號集。

+0

對。我不記得在我的第一個項目中加入這個項目......必須不加理解地複製它。無論如何,謝謝你讓我去。 –

+0

很高興我能幫到你。 –