EditText焦点问题:进入app之后,就弹出软键盘

现象

进入app之后,首界面的EditText就获取焦点。把软键盘弹了出来

解决方案

第一种:父控件添加属性

      在EditText的外层布局,添加如下代码。给EditText添加没有效果

android:focusable="true"
android:focusableInTouchMode="true"

示例如下:

<RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="@dimen/dp_32"
            android:layout_marginStart="@dimen/dp_10"
            android:layout_marginTop="@dimen/dp_10"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:gravity="center_vertical">
        <EditText
                android:id="@+id/et_search_content"
                android:layout_width="@dimen/dp_250"
                android:layout_height="match_parent"
                android:hint="搜索商品"
                android:textSize="@dimen/sp_12"
                android:paddingLeft="@dimen/dp_10"
                android:singleLine="true"
                android:textColor="@color/color_222222"
                android:background="@drawable/search_input_selector"/>
    </RelativeLayout>

第二种:关闭软键盘 (没实践)

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.方法一(如果输入法在窗口上已经显示,则隐藏,反之则显示)

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 plaincopy
imm.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,则表示输入法打开 

转载自这里

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值