android中软键盘遮挡webview的输入框

1.问题:webview中有一个输入框是在webview的底部,当我点击输入框的时候会弹出安卓的软键盘,这个时候我发现软键盘遮挡住了输入框,很不友好,怎么解决呢???

解决方法1:在setContentView(R.layout.webview_activity);之前加上这行代码:

                        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE| WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

可以了。


2.问题:写了一个聊天界面,软键盘弹出的时候把标题栏和上面的部分文字都挤压跑了,找到原因是因为在有这句代码

android:configChanges="keyboardHidden|orientation|screenSize"

android:windowSoftInputMode="stateHidden|adjustPan"

把这两行去掉就行了


3.禁止软键盘在进入activity的时候自动弹出

在 AndroidMainfest.xml中,为要隐藏软键盘的activity添加属性android:windowSoftInputMode="adjustUnspecified|stateHidden"

<

    android:name="cn.idcby.yuchaowang.activity.UserInfoActivity"

    android:screenOrientation="portrait"

    android:windowSoftInputMode="adjustUnspecified|stateHidden|stateHidden"

    />

主要就是这句 android:windowSoftInputMode="adjustUnspecified|stateHidden|stateHidden"


顺便添加一个键盘工具类

public class KeyboardUtils {

private KeyboardUtils() {

throw new UnsupportedOperationException("u can't instantiate me...");

    }

/*

      避免输入法面板遮挡      <p>在manifest.xml中activity中设置

android:windowSoftInputMode="adjustPan"

*/

    /**

    * 动态显示软键盘

    *

    * @param activity activity

*/

    public static void showSoftInput(final Activity activity) {

View view = activity.getCurrentFocus();

        if (view ==null) view =new View(activity);

        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);

        if (imm ==null)return;

        imm.showSoftInput(view, InputMethodManager.SHOW_FORCED);

    }

/**

    * 动态显示软键盘

    *

    * @param view 视图

    */

    public static void showSoftInput(final View view,Activity activity) {

view.setFocusable(true);

        view.setFocusableInTouchMode(true);

        view.requestFocus();

        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);

        if (imm ==null)return;

        imm.showSoftInput(view, InputMethodManager.SHOW_FORCED);

    }

/**

    * 动态隐藏软键盘

    *

    * @param activity activity

*/

    public static void hideSoftInput(final Activity activity) {

View view = activity.getCurrentFocus();

        if (view ==null) view =new View(activity);

        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);

        if (imm ==null)return;

        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);

    }

/**

    * 动态隐藏软键盘

    *

    * @param view 视图

    */

    public static void hideSoftInput(final View view,Activity activity) {

InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);

        if (imm ==null)return;

        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);

    }

/**

    * 切换键盘显示与否状态

    */

    public static void toggleSoftInput(Activity activity) {

InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);

        if (imm ==null)return;

        imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);

    }

/**

    * 点击屏幕空白区域隐藏软键盘

    * <p>根据EditText所在坐标和用户点击的坐标相对比,来判断是否隐藏键盘

    * <p>需重写dispatchTouchEvent

    * <p>参照以下注释代码

    */

    public static void clickBlankArea2HideSoftInput() {

Log.d("tips", "U should copy the following code.");

        /*

@Override

public boolean dispatchTouchEvent(MotionEvent ev) {

if (ev.getAction() == MotionEvent.ACTION_DOWN) {

View v = getCurrentFocus();

if (isShouldHideKeyboard(v, ev)) {

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);

imm.hideSoftInputFromWindow(v.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);

}

}

return super.dispatchTouchEvent(ev);

}

        // 根据EditText所在坐标和用户点击的坐标相对比,来判断是否隐藏键盘private boolean isShouldHideKeyboard(View v, MotionEvent event) {

if (v != null && (v instanceof EditText)) {

int[] l = {0, 0};

v.getLocationInWindow(l);

int left = l[0],

top = l[1],

bottom = top + v.getHeight(),

right = left + v.getWidth();

return !(event.getX() > left && event.getX() < right

&& event.getY() > top && event.getY() < bottom);

}

return false;

}

*/

    }

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。