Android如何禁止向EditText控件中输入内容详解
作者:dwtedx 时间:2021-07-29 05:01:11
前言
在Android开发中经常会遇到EditText控件,而在App开发过程中、遇到了这样一个问题、那就是Android EditText控件如何禁止往里面输入内容?
最开始找到修改版解决方法、但是当想输入的时候就有问题了、可以参考一下、但不建议这样写
EditText editText = (EditText) findViewById(R.id.editText1);
editText.setKeyListener(null);
看到这个问题大家可能有点奇怪了、EditText的功能不就是往上面写入内容吗?再者、如果真要禁止输入文本、在布局文件中添加
android:focusable="false"
或者在代码中使用、不就Ok了?
editText.setFocusable(false)
项目需求是这样的、在EditText后面跟一个CheckBox、勾选CheckBox可以输入、否则不可以输入、另外也有这种需求、比如已经被setText()内容、则需要禁止输入、防止它被修改、
如果没有显示内容、则将EditText设置为可输入状态
经过测试验证:setFocusable方法的效果只有第一次使用时有效、也就是说若在布局文件里面设置:
android:focusable="false"
即使你在代码中设置此控件属性:editText.setFocusable(true);
也不能对它进行编辑、即setFocusable方案不可行、经过摸索得出可行方案利用setInputType来实现、代码如下
editText.setInputType(InputType.TYPE_NULL);//来禁止手机软键盘
editText.setInputType(InputType.TYPE_CLASS_TEXT);//来开启软键盘
应用程序默认为开启状态、特别注意:这种方法也只能禁止软键盘、若手机自带硬键盘、此方案失效
public class EditTextTest extends Activity
{
/** test EditText forbid input function demo */
EditText editText;
boolean flag = true;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
editText = (EditText) findViewById(R.id.editText1);
Button btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
if (flag==true)
{
System.out.println("开启软键盘");
editText.setInputType(InputType.TYPE_CLASS_TEXT);
flag = false;
}else
{
System.out.println("禁止软键盘");
editText.setInputType(InputType.TYPE_NULL);
flag = true;
}
}
});
}
}
来源:http://dditblog.com/itshare_721.html
标签:android,edittext控件,禁止
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
浅谈C#设计模式之代理模式
2023-01-23 14:30:59
spring是如何解析xml配置文件中的占位符
2023-12-02 05:57:12
C#实现飞行棋源码
2023-11-09 11:17:09
Java 二分查找的实现及图例解析
2023-10-28 11:41:57
![](https://img.aspxhome.com/file/2023/9/58699_0s.png)
c#扫描图片去黑边(扫描仪去黑边)
2022-11-06 11:45:55
c#字符串值类型与引用类型比较示例
2021-12-19 04:26:33
SpringBoot+Nacos+Kafka微服务流编排的简单实现
2023-03-21 10:34:53
![](https://img.aspxhome.com/file/2023/3/72293_0s.jpg)
JavaWeb页面中防止点击Backspace网页后退情况
2023-11-10 10:36:36
关于C#泛型列表List<T>的基本用法总结
2022-10-08 16:43:40
自定义视图view使用Canvas实现手写板和涂鸦功能
2022-02-22 17:00:43
![](https://img.aspxhome.com/file/2023/4/106014_0s.gif)
JDK1.8中ArrayList是如何扩容的
2023-01-20 19:04:47
![](https://img.aspxhome.com/file/2023/0/62700_0s.png)
浅谈C#中堆和栈的区别(附上图解)
2022-02-15 14:05:09
![](https://img.aspxhome.com/file/2023/9/80029_0s.gif)
Spring中的aware接口详情
2023-11-29 10:48:29
![](https://img.aspxhome.com/file/2023/9/60689_0s.webp)
SpringBoot使用Redis实现分布式锁
2021-11-06 20:10:22
SpringBoot接口如何对参数进行校验
2021-08-30 17:25:00
![](https://img.aspxhome.com/file/2023/5/74895_0s.png)
Java多线程之条件对象Condition
2021-09-25 15:15:30
![](https://img.aspxhome.com/file/2023/7/79837_0s.png)
Android 和 windows C/C++/QT通讯时字节存储
2022-07-16 06:09:02
java设计模式学习之装饰模式
2021-10-02 04:22:54
![](https://img.aspxhome.com/file/2023/7/121937_0s.jpg)
c#学习之30分钟学会XAML
2022-02-08 03:09:52
![](https://img.aspxhome.com/file/2023/7/89837_0s.png)
死磕 java同步系列之synchronized解析
2023-09-27 10:07:43