Android开发之WebView输入框提示解决办法
作者:roucheng
在做webview应用时,当输入的文字过多时,输入的提示箭头会移动到输入框外,怎么解决这个问题呢?下面小编给大家介绍Android开发之WebView输入框提示解决办法,一起看看吧
做基于WebView应用时,页面上有一个输入框,当输入的文字过多时,超过输入框的行数时,输入框能够滚动,这时间问题来了,输入的提示箭头会移动到输入框外,如何解决这个问题呢,查找chromium源码如下:
void LoadIfNecessary(jobject context) { if (loaded_) return; loaded_ = true; TRACE_EVENT0("browser", "HandleResources::Create"); JNIEnv* env = base::Android::AttachCurrentThread(); if (!context) context = base::android::GetApplicationContext(); left_bitmap_ = CreateSkBitmapFromJavaBitmap( Java_HandleViewResources_getLeftHandleBitmap(env, context)); right_bitmap_ = CreateSkBitmapFromJavaBitmap( Java_HandleViewResources_getRightHandleBitmap(env, context)); center_bitmap_ = CreateSkBitmapFromJavaBitmap( Java_HandleViewResources_getCenterHandleBitmap(env, context)); left_bitmap_.setImmutable(); right_bitmap_.setImmutable(); center_bitmap_.setImmutable(); drawable_horizontal_padding_ratio_ = Java_HandleViewResources_getHandleHorizontalPaddingRatio(env); }
这个函数加载这几个图片,在java端,
private static Bitmap getHandleBitmap(Context context, final int[] attrs) { // TODO(jdduke): Properly derive and apply theme color. TypedArray a = context.getTheme().obtainStyledAttributes(attrs); final int resId = a.getResourceId(a.getIndex(0), 0); final Resources res = a.getResources(); a.recycle(); final Bitmap.Config config = Bitmap.Config.ARGB_8888; final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = false; options.inPreferredConfig = config; Bitmap bitmap = BitmapFactory.decodeResource(res, resId, options); savePic( bitmap); if (bitmap != null) return bitmap; // If themed resource lookup fails, fall back to using the Context's // resources for attribute lookup. if (res != context.getResources()) { bitmap = BitmapFactory.decodeResource(context.getResources(), resId, options); if (bitmap != null) return bitmap; } Drawable drawable = getHandleDrawable(context, attrs); assert drawable != null; final int width = drawable.getIntrinsicWidth(); final int height = drawable.getIntrinsicHeight(); Bitmap canvasBitmap = Bitmap.createBitmap(width, height, config); Canvas canvas = new Canvas(canvasBitmap); drawable.setBounds(0, 0, width, height); drawable.draw(canvas); return canvasBitmap; }
C++中会调用java中的函数getHandleBitmap,这个函数通过 context.getTheme().obtainStyledAttributes 这个函数,从jdk中加载图片资源,显示时,通过GetBitmap函数获取到图像信息,通过layer_->SetBitmap( bitmap)设置显示的内容,函数如下:
const SkBitmap& GetBitmap(ui::TouchHandleOrientation orientation) { DCHECK(loaded_); switch (orientation) { case ui::TouchHandleOrientation::LEFT: return left_bitmap_; case ui::TouchHandleOrientation::RIGHT: return right_bitmap_; case ui::TouchHandleOrientation::CENTER: return center_bitmap_; case ui::TouchHandleOrientation::UNDEFINED: NOTREACHED() << "Invalid touch handle orientation."; }; return center_bitmap_; }
这么分析下来,想从显示下手解决这个问题,似乎不太可能,那只有替换图片资源,而图像资源是在android.jar包中,还有其他办法吗? 分析源码,
public static Drawable getLeftHandleDrawable(Context context) { return getHandleDrawable(context, LEFT_HANDLE_ATTRS); } public static Drawable getCenterHandleDrawable(Context context) { return getHandleDrawable(context, CENTER_HANDLE_ATTRS); } public static Drawable getRightHandleDrawable(Context context) { return getHandleDrawable(context, RIGHT_HANDLE_ATTRS); }
有这几个图像id 信息,是不是可以重载呢,于是添加自己的
<?xml version="1.0" encoding="utf-8"?> <resources> <style name="MyTheme"> <item name="android:textSelectHandleLeft">@drawable/ic_launcher</item> <item name="android:textSelectHandle">@drawable/aa</item> <item name="android:textSelectHandleRight">@drawable/ic_launcher</item> </style> </resources>
替换掉系统的资源,再添加android:theme="@style/MyTheme" 自己的主题风格,问题解决