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());
}
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | <? 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 > |
下面以数字、电话为例讲述EditText怎么设置输入类型,其他类型可以参考InputType类。
1) 只能输入数字
代码如下 | 复制代码 |
EditText et = (EditText) findViewById(R.id.etTest); |
2) 只能输入电话号码
代码如下 | 复制代码 |
EditText et = (EditText) findViewById(R.id.etTest); |
3) 邮箱地址
代码如下 | 复制代码 |
EditText et = (EditText) findViewById(R.id.etTest); |
4) 禁止输入任何文本
代码如下 | 复制代码 |
EditText et = (EditText) findViewById(R.id.etTest); |
// 禁止输入(不弹出输入法)上述也是隐藏输入法的一种方式,还有另外一种隐藏办法,
可查看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); |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
2016-01-31 Android 测试自定义纯数字软键盘