解决EditText导致输入法自动弹出的几种方案

来源:互联网 发布:淘宝滔搏运动城 编辑:程序博客网 时间:2024/06/05 15:45

方案一 :  使用输入法管理器进行隐藏

InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);inputMethodManager.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);

getWindowToken()的方式还有其他几种, 如:

etSearch.getWindowToken()  //用控件对象来获取activity.getCurrentFocus().getWindowToken() //可能会空指针异常
是否有效需要具体测试, 本人在几个项目中用过这个方案, 有时就无法隐藏


方案二 : 让其他控件获取焦点

tvSearch.requestFocus();

该方案本人测试过两次, 一次有效一次无效


方案三 : 在EditText的父控件添加focusableInTouchMode属性

    <RelativeLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:focusableInTouchMode="true">        <EditText            android:id="@+id/et_search"            android:layout_width="match_parent"            android:layout_height="wrap_content"/>        <ImageView            android:id="@+id/iv_search"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_alignParentRight="true"            android:layout_centerVertical="true"            android:layout_marginRight="10dp"            android:src="@drawable/selector_btn_search"/>    </RelativeLayout>




0 0
原创粉丝点击