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. HDU 6112 今夕何夕 蔡勒公式

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6112题意:中文题目 分析:关键点在与如何计算一个日期是星期几,这个可以通过蔡勒公式来计算.基姆拉尔森计 ...

  2. UVALive 5099

    B - Nubulsa Expo Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit S ...

  3. 微信openid和UnionID (多公众号如何判断是否是同一人)

    以下内容源于网络,因为非博客园信息,有想收藏,只能直接复制粘贴了,还希望原稿人员理解. 微信公众平台更新,为开发者提供UnionID机制 经开发者反馈,由于同一公司下多个公众号之间需要用户帐号互通,微 ...

  4. 打印之Lodop

    前序 前面遇到一个问题:在线打印合同.通过各方查找资料和请教他人,终于完美的解决了这个问题.其中的解决方案,可以查看:http://www.cnblogs.com/zcy-xy/p/4290436.h ...

  5. web前端-《手机移动端WEB资源整合》——meta标签篇

    前端网页meta元素可提供有关页面的元信息(meta-information),比如针对搜索引擎和更新频度的描述和关键词.meta标签的作用有:搜索引擎优化(SEO),定义页面使用语言,自动刷新并指向 ...

  6. bzoj 1040 基向内环树dp

    #include<bits/stdc++.h> #define LL long long #define fi first #define se second #define mk mak ...

  7. CentOS7单机部署lamp环境和apache虚拟主机

    (1)apache介绍 apache : httpd.apache.org 软件包:httpd 端口服务:80/tcp(http) 443/tcp(https,http+ssl) 配置文件: /etc ...

  8. logrotate日志轮转

    1)基本介绍 适合应用服务日志,系统日志按天切割 如果没有日志轮转,日志文件会越来越大 将丢弃系统中最旧的日志文件,以节省空间 logrotate本身不是系统守护进程,它是通过计划任务crond每天执 ...

  9. Codeforces 1082 B. Vova and Trophies-有坑 (Educational Codeforces Round 55 (Rated for Div. 2))

    B. Vova and Trophies time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  10. 【转载】Scroller源码解析

    原文地址:https://github.com/Skykai521/AndroidSdkSourceAnalysis/blob/master/article/Scroller%E6%BA%90%E7% ...