Nikniklook 2021-05-14 21:58 采纳率: 0%
浏览 254

android 软键盘弹出,隐藏虚拟按键跳出留有空白怎么解

package com.example.dialogact;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

public class DialogActivity extends AppCompatActivity {
    private static final String TAG = "tag_dia";
    EditText edt ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {


        super.onCreate(savedInstanceState);
        hideBottomUIMenu();
        setContentView(R.layout.activity_dialog);
        edt = findViewById(R.id.tel);
        Window win = this.getWindow();
        Log.d(TAG, "onCreate: " + ScreenUtil.getNevirgation(this));
        WindowManager.LayoutParams lp = win.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.gravity = Gravity.CENTER;//设置对话框置底部显示
        win.setAttributes(lp);
        edt.requestFocus();
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);


    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // TODO Auto-generated method stub
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            //获取Activity上View 的焦点不等于空,获取焦点的标志不等于空
            if (getCurrentFocus() != null && getCurrentFocus().getWindowToken() != null) {

                //获取软键盘服务
                InputMethodManager manager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                //管理器隐藏在windows上的软键盘(标记的VIEW,如果输入法在窗口上已经显示,则隐藏,反之则显示)
                manager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
                hideBottomUIMenu();
            }
        }
        return super.onTouchEvent(event);
    }

    protected void hideBottomUIMenu() {
        //隐藏虚拟按键,并且全屏
        if (Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT < 19) { // lower api
            View v = this.getWindow().getDecorView();
            v.setSystemUiVisibility(View.GONE);
        } else if (Build.VERSION.SDK_INT >= 19) {
            //for new api versions.
            View decorView = getWindow().getDecorView();
            int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_FULLSCREEN;
            decorView.setSystemUiVisibility(uiOptions);

        }
    }
}

 

  • 写回答

1条回答 默认 最新

  • 无限虚空 2024-06-22 20:12
    关注

    webapp中,input表单focus会触发虚拟键盘弹出,blur会触发虚拟键盘隐藏。

    但是有时会出现,blur触发了虚拟键盘隐藏,但是虚拟键盘的背景区域依然保留在原来的位置。

    点击body任何区域,或者在页面中 alert() 阻塞一下,灰色背景就消失了;但是点击或者alert(),都需要用户再次操作,严重影响用户体验。

    因此,在键盘隐藏后阻塞一下,改变可视区域可以让灰色背景隐藏,也不影响用户体验。

     

    这里是通过可视区域的高度来判断虚拟键盘的显示与隐藏。

    js代码如下:

    var clientHeight = document.documentElement.clientHeight || document.body.clientHeight;
    $(window).on('resize', function () {
        var nowClientHeight = document.documentElement.clientHeight || document.body.clientHeight;
        if (clientHeight > nowClientHeight) {
            //键盘弹出的事件处理
            $('.paymentWrap').css({ 'position': 'static' }); // 取消支付按钮绝对定位,避免它移动到虚拟键盘上方
        }
        else {
            //键盘收起的事件处理
            $('.paymentWrap').css({ 'position': 'fixed' });  // 恢复支付按钮绝对定位
            $('body').height(nowClientHeight-1);   // 改变body的高度
            // 阻塞一下才有效果
            setTimeout(function() {
                $('body').height(nowClientHeight); // 恢复body的高度
            }, 0);
        }
    });
    
    
    评论

报告相同问题?