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

edittext 手机号、邮箱输入限制的更多相关文章

  1. Android EditText手机号格式化输入XXX-XXXX-XXXX

    先来效果图: 设置手机格式化操作只需要设置EditText的addTextChangedListener的监听,下面看代码 /*editText输入监听*/ et_activity_up_login_ ...

  2. Android限定EditText的输入类型为数字或者英文(包括大小写),EditText,TextView只能输入两位小数

    Android限定EditText的输入类型为数字或者英文(包括大小写) // 监听密码输入框的输入内容类型,不可以输入中文    TextWatcher mTextWatcher = new Tex ...

  3. jquery 实现邮箱输入自动提示功能

    邮箱的广泛使用得益于它的免费,因此很多网站在注册的时候都会直接使用邮箱作为账号名 为了提高用户的体验,很多网站都会实现邮箱输入的自动提示功能,所有自己也实现了一个,先看下效果吧,觉得效果还行的就拿去 ...

  4. jquery 实现邮箱输入自动提示功能:(二)

    上篇文章写到了一个不错的jquery实现邮箱输入自动提示功能,发现还有一个不错的自动提示插件: 先展示结果如图: html代码: <center> <h1>输入邮箱试试!< ...

  5. jquery 实现邮箱输入自动提示功能:(一)

    记得去年做某个项目的时候,用到了邮箱输入自动提示功能,于是网上搜了一下,发现了这个写得不错,现在回想起来,转载一下,方便查阅. 邮箱的广泛使用得益于它的免费,因此很多网站在注册的时候都会直接使用邮箱作 ...

  6. Android 限制EditText仅仅能输入数字、限制输入类型、限制输入长度的小技巧

    准确的说让Edittext仅仅能输入数字有方法两种,都是通过xml属性设置 方法一: <EditText android:id="@+id/u_account" androi ...

  7. android EditText控制最大输入行数

    网络摘抄,仅作记录学习 EditText在android开发中是一个经常用到的基础控件,功能也很强大,限制输入字符类型,字数什么的.但是最近在工作中遇到了需要控制editText最大可输入行数的要求. ...

  8. 模拟邮箱输入邮箱地址、收藏标签。input框输入内容后回车,内容显示成小方块并带删除按钮。

    模拟邮箱输入邮箱地址.收藏标签: 文本框输入文字后按回车键或者分号键,输入框中的文字变成小块并带删除按钮和操作. 页面代码: <!DOCTYPE html> <%@ page lan ...

  9. [博主推荐]如何利用注册 的 bug 来疯狂注册,不停开小号"做"事情,支持 手机号&邮箱

    [博主推荐]如何利用注册 的 bug 来疯狂注册,不停开小号"做"事情,支持 手机号&邮箱  非常简单 1.手机号注册: 用手机号注册 网站基本都支持 可以用推荐的网址: ...

随机推荐

  1. 解决TextView多行滑动与NestedScrollView等,滑动冲突,我的解决方案

    1.首先要明白,什么时候回TextView处理滑动,什么时候不处理滑动 1.1往上滑动,到达文本底部就不要再处理了,如果往上滑动不在底部则继续TextView滑动 1.2往下滑动,到达文本顶部就不要再 ...

  2. 在一个Ubuntu系统上配置Apache支持多个站点

    查看原文请访问:http://codewenda.com/ubuntu16-04%E9%85%8D%E7%BD%AEapache%E6%94%AF%E6%8C%81%E5%A4%9A%E4%B8%AA ...

  3. leetcode 之Gas Station(11)

    这题的思路很巧妙,用两个变量,一个变量衡量当前指针是否有效,一个衡量整个数组是否有解,需要好好体会. int gasStation(vector<int> &gas, vector ...

  4. php性能的问题

    一.影响php性能的常见原因 1.php自身语法使用不当 2.php做了不擅长的时期() 3.php的周边环境(服务器Linux,磁盘:文件存储,数据库,缓存:内存,网络:带宽) 4.php自身的短板 ...

  5. java中String的内存位置

  6. html,图片上传预览,input file获取文件等相关操作

    input file常用方法: var obj=document.getElementById("upimage"); var file=obj.files[0];//获取文件数据 ...

  7. 自定义wordCount程序、

    1.MyWordCount代码: package com.hadoop.mr; import java.io.IOException; import org.apache.hadoop.conf.Co ...

  8. Java学习笔记(八)——java多线程

    [前面的话] 实际项目在用spring框架结合dubbo框架做一个系统,虽然也负责了一块内容,但是自己的能力还是不足,所以还需要好好学习一下基础知识,然后做一些笔记.希望做完了这个项目可以写一些dub ...

  9. ajax在提交url时候遇到的编码问题

    //escape()不能直接用于URL编码,它的真正作用是返回一个字符的Unicode编码值.比如"春节"的返回结果是%u6625%u8282,escape()不对"+& ...

  10. Openstack 清除openstack网络与路由 (十七)

    一)清除openstack网络与路由 “清除openstack网络与路由”和”添加openstack网络与路由”的操作步骤相反. 添加网络或路由时是先建 搭建网络>搭建子网>建立端口, 而 ...