Android中WebView输入框不弹起软盘问题记录

Android中WebView输入框不弹起软盘问题记录

最近项目中对接一个WebView网页,其中的JS交互和同事对好数据和规则后都很顺利,接近完成时突然发现点击网页的输入框无法弹出软盘进行输入;在网上找了各种方法尝试,最终还是解决了,记录一下:

网上说的基本上有这几种,可能大家遇到的情况不同,解决方法也都有区别

1.给webview获取焦点

webView.setEnabled(true);  //  这里如果设置false, 则点击h5页面中的输入框时不能唤起软键盘
webView.requestFocus();
//或者
webview.requestFocus(View.FOCUS_DOWN);
//还有说加上这个
webview.requestFocusFromTouch() ;

上面的方法在我这设置是无效的

2.重写 WebView的onInterceptTouchEvent方法

    override fun onInterceptTouchEvent(ev: MotionEvent): Boolean {
        val v: View = this
        when (ev.action) {
            MotionEvent.ACTION_DOWN, MotionEvent.ACTION_UP -> v.requestFocusFromTouch() 
            MotionEvent.ACTION_MOVE -> {}
            MotionEvent.ACTION_CANCEL -> {}
        }
        return false
    }

这个确实是能弹起软盘并输入的,可是它是在每次点击时给webView设置了requestFocusFromTouch,导致点击时页面会闪,而且滑动页面影响很大,滑动选择就基本不能用,所以这种最好就别用

3.继承webView时构造方法defStyleAttr参数造成的

这是我的有问题的构造方法

class MyWebView : WebView {

    private val TAG = "PointCloudView"

    constructor(context: Context) : super(context, null)

    constructor(context: Context, attributeSet: AttributeSet?) : super(context, attributeSet, -1)

    constructor(context: Context, attributeSet: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attributeSet,
        defStyleAttr
    )
}

修改后

class MyWebView : WebView {

    constructor(context: Context) : super(context, null)

    constructor(context: Context, attributeSet: AttributeSet?) : super(context, attributeSet)

    constructor(context: Context, attributeSet: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attributeSet,
        defStyleAttr
    )
}

就是继承WebView后构造方法里面没有defStyleAttr参数时不能填0或-1之类的。
具体这个参数的作用并没有去了解

最终是第三种方法解决了问题,其实还有在点击事件分发时强制每个人遇到的可能都不同,可以多试几种

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值