Android:input 輸入框比較靠頁面下部,輸入時鍵盤會遮擋input輸入框,處理方法.
使用系統自帶鍵盤時,隻安卓會遮蓋輸入框,原因: 頁面高度由'視口高度'變成了'視口高度 - 鍵盤高度'.
使用antd-mobile money自帶鍵盤時,安卓和iOS都會遮蓋輸入框.
解決方法:
1. input聚焦時,頁面外框設定margin-bottom: 200px(鍵盤的高度),//使用系統自帶鍵盤時不需要設定此項
2.計算可以使輸入框進入可視區的scrollTop值,
3.設定容器scrollTop值讓輸入框進入可視區.
import React, { FC, useEffect, useState, useRef } from 'react';
import { InputItem as AntdInputItem } from 'antd-mobile';
const InputComponent: FC<any> = (props) => {
const [amountInput, setAmountInput] = useState<number | undefined>(undefined);
const [isFocus, setIsFocus] = useState<boolean>(false);
// const windowRef = useRef<any>();
useEffect(() => {
if(props.gasId && props.oilNumber !== undefined) {
getAmounts();
// 記錄初始視窗高度
// windowRef.current = {
// outerHeight: window.outerHeight,
// innerHeight: window.innerHeight,
// topN: Math.floor(120/667*window.innerHeight) || 100
// };
// console.log('windowRef.current',windowRef.current);
// window.addEventListener('resize', windowResize); // 監聽安卓手動關閉系統鍵盤操作
}
// return () => {
// window.removeEventListener('resize', windowResize);
// };
}, [props.gasId, props.oilNumber]);
// const windowResize = (e) => {
// console.log('resize .outerHeight .innerHeight', e.target.outerHeight, e.target.innerHeight);
// // 監聽安卓手動關閉系統鍵盤操作
// if(e.target.outerHeight < windowRef.current?.outerHeight || e.target.innerHeight < windowRef.current?.innerHeight) {
// setContainerScrolltop();
// }
// };
const setContainerScrolltop = () => {
//使用系統自帶鍵盤時,隻需設定安卓的即可
//const u = navigator.userAgent;
//const isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android終端
//if(isAndroid) {
// 使用antd-mobile InputItem['money']的鍵盤時,安卓和iOS都需要設定scrollTop
const container = document.querySelector('#container');//設定了overflow-y:auto;的外容器
const inputContent = document.querySelector('#inputContent');//input元素(由于antd-mobile InputItem不能設定id,是以加了個外框)
const wrapOffsetTop = container?.offsetTop; // 容器到頁面頂部的距離
const inputOffsetTop = inputContent?.offsetTop; // input框到頁面頂部的距離
const sTop = inputOffsetTop - wrapOffsetTop; //input框到容器頂部的距離,滾動距離設定為此值,即可使input剛好出現在容器頂部
console.log('sTop', sTop);
// 使用 antd-mobile money 自帶的鍵盤時,安卓,iOS都需要滑動
document.querySelector('#refuelDetailContainer')?.scrollTo(0, sTop);
console.log('滑出高度',container?.scrollTop);
// }
};
const inputOnFocus = () => {
setIsFocus(true);
setTimeout(() => {
setContainerScrolltop();
}, 300);
};
const inputOnBlur = () => {
setIsFocus(false);
console.log('blur');
};
const inputChange = (val) => {
const val = value;
// 隻能輸入整數或2位小數
const isMatch = val.match(/^(\d{0,12}|0)((\.{0,1}\d{0,2})?)$/);
if(isMatch) {
console.log('test true');
}else {
return false;
}
if(!String(val).length){
setAmountInput(undefined);
return false;
}
setAmountInput(val);
}
return (
<div id="container" className={classnames(styles.container, isFocus ? styles.containerMargin : '')}>
<div style={{width: '100%', height: '500px', background: 'lightblue'}}></div>
<div id="inputContent">
<AntdInputItem
type={'money'}
placeholder=""
value={amountInput}
extra={'金額'}
maxLength={15}
onChange={inputChange}
onFocus={inputOnFocus}
onBlur={inputOnBlur}
autoAdjustHeight
>
{amountInput !== undefined ? '¥' : ''}
</AntdInputItem>
</div>
</div>
);
}
export default InputComponent;
.containerMargin {
margin-bottom: 200px;
}