您的位置:首页 > 移动开发

Android webview 键盘遮住输入框

2015-08-17 12:02 309 查看
该文章纯属原创, 转载请说明出处

开发Android 一年多了,一直都是在网上找资料,现在有时间我也整理一个我遇到的问题,我的处女篇啊!!

相信很多Android开发者都遇到webview调用键盘然后输入框被遮住,效果很丑,体验很差。然后会疯狂的在网上找资料,在网上大致有四种方法(都不靠谱),唯一的一个就是设置android:windowSoftInputMode=”adjustResize”,然而却是太丑了。接下来我要介绍一种暴力的方法,该方法用到类Scroller,如果不懂这个类请上网查资料。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<com.example.cweb.MyLinearLayout
android:id="@+id/my_linear"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<WebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</com.example.cweb.MyLinearLayout>

</LinearLayout>


这个是放webview的布局,MyLinearLayout很简单就是继承了Linearlayout,它里面的主要的方法就是滑动,

/**
* 滚动到目标位置
*
* @param fx
* @param fy
*/
protected void smoothScrollTo( int fx, int fy ) {

int dx = fx - mScroller.getFinalX();
int dy = fy - mScroller.getFinalY();
smoothScrollBy( dx, dy );
}

/**
* 滑动到相对的位置
*
* @param dx
* @param dy
*/
protected void smoothScrollBy( int dx, int dy ) {

// 设置mScroller的滚动偏移量
mScroller.startScroll( mScroller.getFinalX(), mScroller.getFinalY(), dx, dy );
invalidate();// 这里必须调用invalidate()才能保证computeScroll()会被调用,否则不一定会刷新界面,看不到滚动效果
}

@Override
public void computeScroll() {

// 判断mScroller滚动是否完成
if( mScroller.computeScrollOffset() ) {
// 这里调用View的scrollTo()完成实际的滚动
scrollTo( mScroller.getCurrX(), mScroller.getCurrY() );
// 必须调用该方法,否则不一定能看到滚动效果
postInvalidate();
}
super.computeScroll();
}


两个滑动的方法smoothScrollBy和smoothScrollTo,这个代码相信不难, so easy的。

接下来就是启动滑动的代码了,

webView.setOnTouchListener( new OnTouchListener() {

@Override
public boolean onTouch( View v, MotionEvent event ) {

if( event.getAction() == MotionEvent.ACTION_UP ) {
pointY = ( int )event.getY();
clickTime = System.currentTimeMillis();

}
return false;
}
} );
final View mainLinear = findViewById( R.id.main_layout );

mainLinear.getViewTreeObserver().addOnGlobalLayoutListener( new OnGlobalLayoutListener() {

@Override
public void onGlobalLayout() {

int preHeight = 0;
int heightDiff = mainLinear.getRootView().getHeight() - mainLinear.getHeight();

if( preHeight == heightDiff ) {
Log.e( "MainActivity", "++" );
return;
}
preHeight = heightDiff;
// 需要客户端启动滑动的初始Y坐标
int startY = mScreenHeight / 2;
if( heightDiff >= 50 && !isOpenKeyBord ) {
if( pointY < ( startY - 200 ) ) {
return;
}
//必须在监听到点击事件600ms之内才进行滑动,防止刚刚进来直接往上滑动
if( ( System.currentTimeMillis() - clickTime ) < 600 ) {
int y1 = Math.abs( pointY - startY ) + 200;
Logs.e( tag, "OnGlobalLayoutListener界面上移" + y1 );
myLinearLayout.smoothScrollBy( 0, y1 );
isOpenKeyBord = true;
}

} else {
Logs.e( tag, "OnGlobalLayoutListener界面还原" );
myLinearLayout.smoothScrollTo( 0, 0 );
isOpenKeyBord = false;

}
preHeight = 0;
}

} );


mainLinear 是整个layout 的view,这个方法是根据布局界面变化来判断键盘的显示和关闭的

heightDiff >= 50 这个值究竟放多少要自己研究,我在手机上市100,但是在小米平板上用50,你究竟用多少还是自己测吧。这个方法就是键盘调起的时候把界面往上滑动,键盘关闭的时候把键盘还原。是不是很简单粗暴,

还有一点要注意,在manifest注册时一定 android:theme=”@android:style/Theme.NoTitleBar”,

千万别写这样 android:theme=”@android:style/Theme.NoTitleBar.Fullscreen”,不然你会奔溃的,当然你要其他的titlebar就用其他的,哈哈

这样,你就可以实现调用键盘滑动了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android webview 键盘