uin-app简明uniapp教程

软键盘弹出和收起逻辑说明

2019-12-06  本文已影响0人  瑟闻风倾

1. 软键盘弹出的逻辑说明

(1) 逻辑说明:App平台软键盘弹出有 adjustResize|adjustPan 两种模式,默认为 adjustPan 模式,小程序平台只支持 adjustPan 模式,H5平台因不同浏览器而异

(2) 配置方式,在 pages.json 中配置 style

"app-plus": {
    "softinputMode": "adjustPan"
}

注意:adjustResize模式在Android App上,弹起键盘和收回键盘时,因为要重设webview窗体高度,可能会在个别安卓机型闪现灰屏或漏出下层页面内容。
疑问:HbuilderX升级后,虽然softinputMode设为adjustPan,但输入框聚焦软键盘弹出时页面仍被挤压。可通过给被压缩的区域设置一个最小高度(即一个能让所有元素按原来布局排列的高度)来解决页面变形问题,参考手机软键盘弹起导致页面变形的一种解决方案
示例:设置中间被压缩区域最小高度min-height: 300upx;

<template>
    <view class="uni-flex uni-column" style="width: 100%;height: 100%;">
        <view class="uni-flex uni-row justify-end align-center" style="width: 100%;height: 14%;">1.顶部区域</view>
        
        <view class="uni-flex uni-row justify-center align-center" style="width: 100%;height: 72%;min-height: 300upx;">
            <view class="flex-treble uni-flex justify-center align-center" style="height: 100%;">2.1中间左边区域</view>
            
            <view class="flex-five uni-flex uni-column justify-center align-center" style="height: 100%;">
                <view class="flex-treble uni-flex uni-row justify-center align-center" style="width: 100%;">2.2主区域-(1)顶部</view>

                <view class="flex-four uni-flex uni-column justify-center align-center" style="width: 100%;">
                    <view class="flex-sub uni-flex uni-column justify-start align-center padding-lr-15" style="width: 100%;">
                        <view class="uni-flex uni-row justify-start align-center" style="width: 100%;">
                            <image class="icon_user" src="../../static/icon_user.png"></image>
                            <input v-model="account" placeholder="输入用户名"></input>
                        </view>
                        <view class="shape_line"></view>
                    </view>

                    <view class="flex-sub uni-flex uni-column justify-start align-center padding-lr-15" style="width: 100%;">
                        <view class="uni-flex uni-row justify-start align-center" style="width: 100%;">
                            <image class="icon_password" src="../../static/icon_password.png"></image>
                            <input password v-model="password" placeholder="输入密码"></input>
                        </view>
                        <view class="shape_line"></view>
                    </view>
                </view>

                <view class="flex-twice uni-flex uni-row justify-center align-center padding-lr-15" style="width: 100%;">
                    <button class="flex-sub  @tap="userLogin">登录</button>
                    <view class="flex-sub">2.2主区域-(3)底部</view>
                    <button class="flex-sub">注册帐号</button>
                </view>
            </view>

            <view class="flex-sub uni-flex  justify-center align-center" style="height: 100%;">2.3中间右边区域</view>
        </view>
        
        <view style="width: 100%;height: 14%;">3.底部区域</view>
        
    </view>
</template>

2. 软键盘收起的逻辑说明

(1) 逻辑说明

以上为默认逻辑,uni-app同时提供了隐藏软键盘的api:uni.hideKeyboard()

uni.hideKeyboard();//隐藏软键盘
// plus.key.hideSoftKeybord();

注意:它只隐藏已经显示的软键盘,如果软键盘没有显示则不做任何操作。在实际应用中,如外接扫码枪时,不能通过该api来实现完全禁用软键盘的效果。

focus:function(){
    console.log("输入框聚焦");
    // 并不能完全禁用软键盘
    setTimeout(function(){
        uni.hideKeyboard();//隐藏软键盘
        // plus.key.hideSoftKeybord();
    },250);
            
},
上一篇 下一篇

猜你喜欢

热点阅读