最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
Android如何禁止向EditText控件中输入内容详解
时间:2022-06-25 23:20:45 编辑:袖梨 来源:一聚教程网
在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;
}
}
});
}
}
相关文章
- GitHub Copilot开发者最佳实践:团队协作中5项检查清单 06-11
- 云上智农app如何申报农民专项信贷 06-11
- 京东白拿活动入口 - 2026最新免费领商品攻略 06-11
- Cursor开发者进阶技巧:如何用代码审查功能提升团队效率? 06-11
- Cursor怎么低成本使用?2026年3个免费技巧 06-11
- Cursor开发者报错怎么解决?3种排查方法 06-11