Android中使EditText失去焦点,edittext禁止弹出键盘[转]
转自http://www.cnblogs.com/yejiurui/archive/2013/01/02/2841945.html
在我们的应用中,有时候一进入一个页面, EditText默认就会自动获取焦点。弹出输入法框,用户体验很不好,
那么如何取消这个默认行为呢?
ps:这篇文字是一年前写的,现在有网友再问这个问题,我进行重新编辑--2014.05.07,目前有更好的办法,第一种方法局限性很强,大家可以使用第二种方法
第一种方法:.在网上找了好久,有点监听软键盘事件的方法,有调用 clearFouse()方法,但是测试了都不行!在对应的 xml中也找不到相应的属性可以关闭这个默认行为。
后来研究了一下,在其父控件下,添加如下的属性,就可以完美解决:
android:focusable="true"
android:focusableInTouchMode="true"
举例如下:

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:focusable
="true" android:focusableInTouchMode="true"
>
<EditText
android:id="@+id/et_enter_msg_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<Button
android:id="@+id/sent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/send"
/>
</LinearLayout>

第二种方法:直接关闭输入法
|
1
2
3
4
5
6
7
8
|
private void closeInputMethod() { InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); boolean isOpen = imm.isActive(); if (isOpen) { // imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);//没有显示则显示 imm.hideSoftInputFromWindow(mobile_topup_num.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); }} |
调用这个方法体就行了,具体if语句里面的几个参数,我就借用一个网友的日志来写把(在此感谢)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
1、方法一(如果输入法在窗口上已经显示,则隐藏,反之则显示) InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS); 2、方法二(view为接受软键盘输入的视图,SHOW_FORCED表示强制显示) InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.showSoftInput(view,InputMethodManager.SHOW_FORCED); [java] view plaincopyimm.hideSoftInputFromWindow(view.getWindowToken(), 0); //强制隐藏键盘 3、调用隐藏系统默认的输入法 ((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(WidgetSearchActivity.this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); (WidgetSearchActivity是当前的Activity) 4、获取输入法打开的状态 InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); boolean isOpen=imm.isActive();//isOpen若返回true,则表示输入法打开 |
Android中使EditText失去焦点,edittext禁止弹出键盘[转]的更多相关文章
- Android进入一个新页面,EditText失去焦点并禁止弹出键盘
android在进入一个新页面后,edittext会自动获取焦点并弹出软键盘,这样并不符合用户操作习惯. 在其父控件下,添加如下的属性,就可以完美解决,使其进入页面后不主动获取焦点,并且不弹出软键盘: ...
- UITableView中cell里的UITextField不被弹出键盘挡住
UITableView中cell里的UITextField不被弹出键盘挡住 本人视频教程系类 iOS中CALayer的使用 效果如下: 源码: EditCell.h 与 EditCell.m // ...
- android中RecyclerView控件实现长按弹出PopupMenu菜单功能
之前写过一篇文章:android中实现简单的聊天功能 现在是在之前功能的基础上,添加一个长按聊天记录,删除对应聊天记录的功能 RecyclerView控件,没有对应的长按事件,我们需要自己手工添加,修 ...
- :input获得焦点时被弹出键盘挡住解决办法
这个是移动端非常常见的bug了,这里说下综合的解决办法,因为有时候你的办法就是会失效.. 上代码 /*input框调起输入法盖住输入问题*/$('input[type="text" ...
- 关于Android中EditText自动获取焦点并弹出键盘的相关设置
在android开发中,关于EditText自动获取焦点弹出键盘,我们可能又是会有让键盘自动弹出的需求,有时可能又会有不想让键盘自动弹出的需求,下面是我所总结的两种方法: 需求:EditText自动获 ...
- EditText: EditText自动获取焦点并弹出键盘&EditText不自动获取焦点并且不会弹出键盘
1.EditText不自动获取焦点并且不会弹出键盘 找到EditText的父控件,设置其父控件为: Parent.setFocusable(true); Parent.setFocusableInTo ...
- android 闹钟提醒并且在锁屏下弹出Dialog对话框并播放铃声和震动
android 闹钟提醒并且在锁屏下弹出Dialog对话框并播放铃声和震动 1.先简单设置一个闹钟提醒事件: //设置闹钟 mSetting.setOnClickListener ...
- sublimeText3安装package control和禁止弹出更新下载弹窗
1.sublimeText3安装package control import urllib.request,os; pf = 'Package Control.sublime-package'; ip ...
- JS弹出层制作,以及移动端禁止弹出层下内容滚动,overflow:hidden移动端失效问题
HTML <div class="layer"> <div class="menu-list"> <span>社会</ ...
随机推荐
- poj 1681 Painter's Problem
Painter's Problem 题意:给一个n*n(1 <= n <= 15)具有初始颜色(颜色只有yellow&white两种,即01矩阵)的square染色,每次对一个方格 ...
- .where(provider).FirstOrDefault()和.FirstOrDefault(provider)的性能比较
最近遇到一个关于Linq的问题,.where(provider).FirstOrDefault();和.FirstOrDefault(provider);的性能比较 关于这个主要有以下三种说法,但这方 ...
- C#中通过调用Dll函数时,执行一段时间后,就会报内存可能被破坏的错的解决办法
遇到同样的问题,已经解决的:http://blog.csdn.net/youxiazzz12/article/details/24313347
- Python 类 setattr、getattr、hasattr 的使用
#coding=utf-8 class Employee: '所有员工的基类' empCount = 0 def __init__(self, name, salary): self.name = n ...
- hdu 4722
比赛的时候这道题一直都没出来,承启提醒我之后还是一直WA: 其实规律早就找到了```` 其实这题还可以用数位dp来做,不过从来没写过,以后再贴: 代码: #include<iostream> ...
- Cloud Insight 客户案例-晨芯时代科技有限公司
在不断迭代的过程中,Cloud Insight 也很重视客户对产品的使用体验,这次我们拜访了晨芯时代,了解到他们在使用 Cloud Insight 过程中对产品的一些想法. 客户背景 晨芯时代是一家开 ...
- 【Uvalive4960】 Sensor network (苗条树,进化版)
[题意] 给出N个点,M条边,问这N个点形成的生成树的最大权值边-最小权值边的最小值 InputThe input consists of several test cases, separated ...
- Jquery 获取表单值如input,select等方法
1 if($("input[name=item][value='val']").attr('checked')==true) //判断是否已经打勾 name即控件name属性,va ...
- 【HDOJ】1072 Nightmare
bfs,使用ttl进行剪枝. #include <iostream> #include <cstdio> #include <cstring> #include & ...
- BZOJ2038小Z的袜子(hose)
2038: [2009国家集训队]小Z的袜子(hose) Time Limit: 20 Sec Memory Limit: 259 MBSubmit: 2343 Solved: 1077[Subm ...