edittext 手机号、邮箱输入限制

 
 
package com.example.yanlei.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

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

}
/*显示点击的内容*/
private void showClickMessage(String message) {
Toast.makeText(MainActivity.this, "你选择的是: " + message, Toast.LENGTH_LONG).show();
}

public void click(View view) {
EditText et = (EditText) findViewById(R.id.editText);
et.setInputType(InputType.TYPE_CLASS_NUMBER);
//Button btn_Button = (Button)(view);
//showClickMessage("ok" + btn_Button.getText());

}
}
<?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.example.yanlei.myapplication.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="我爱你 YL!"
        android:textStyle="normal|bold|italic"
        android:textColor="@color/colorAccent" />

    <TextView
        android:text="TextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="49dp"
        android:layout_marginStart="49dp"
        android:id="@+id/textView2" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:text="Name"
        android:ems="10"
        android:layout_below="@+id/textView"
        android:layout_marginTop="66dp"
        android:id="@+id/editText" />

    <Button
        android:text="Button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/textView2"
        android:layout_toRightOf="@+id/textView"
        android:layout_toEndOf="@+id/textView"
        android:layout_marginBottom="31dp"
        android:onClick="click"
        android:id="@+id/button2" />
</RelativeLayout>

  

 
 
 
 
本文章来给大家介绍在android开发中我们要对EditText限制,只能让用户输入像数字 字母 邮箱地址,电话号之类的,其它的不能输入。
 

下面以数字、电话为例讲述EditText怎么设置输入类型,其他类型可以参考InputType类。

1) 只能输入数字

 代码如下 复制代码

EditText et = (EditText) findViewById(R.id.etTest);
et.setInputType(InputType.TYPE_CLASS_NUMBER);

2) 只能输入电话号码

 代码如下 复制代码

EditText et = (EditText) findViewById(R.id.etTest);
et.setInputType(InputType.TYPE_CLASS_PHONE);//电话

3) 邮箱地址

 代码如下 复制代码

EditText et = (EditText) findViewById(R.id.etTest);
et.setInputType(InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);

4) 禁止输入任何文本

 代码如下 复制代码

EditText et = (EditText) findViewById(R.id.etTest);
et.setInputType(InputType.TYPE_NULL);

// 禁止输入(不弹出输入法)上述也是隐藏输入法的一种方式,还有另外一种隐藏办法,

可查看android隐藏IME(输入法)输入框

不让程序默认升起IME输入框有两种方法: 
1.让EditText失去焦点,使用EditText的clearFocus方法 
2.强制隐藏Android输入法窗口,在IME类中我们通过实例化输入法控制对象,通过hideSoftInputFromWindow来隐藏IME输入框。

代码

 

 

 代码如下 复制代码
Toast.makeText(WindowBackgroundColorActivity.this, "焦点改变", Toast.LENGTH_SHORT).show(); 
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 
//第一种方法 
//imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, InputMethodManager.HIDE_NOT_ALWAYS); 
//第二种方法 
imm.hideSoftInputFromWindow(v.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);