Android中使EditText失去焦点禁止弹出键盘


在我们的应用中,有时候一进入一个页面, EditText默认就会自动获取焦点。弹出输入法框,用户体验很不好,那么如何取消这个默认行为呢? 
目前有更好的办法,第一种方法局限性很强,大家可以使用第二种方法  
第一种方法: 

在其父控件下,添加如下的属性,就可以完美解决: 

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>  

第二种方法:直接关闭输入法 

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); 
    } 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值