Android Development WebView Input Box Prompt Solution

  • 2021-07-18 08:59:59
  • OfStack

When doing WebView application, there is an input box on the page. When the input text is too much, when it exceeds the number of lines in the input box, the input box can scroll. This time problem comes, and the input prompt arrow will move outside the input box. How to solve this problem, find the source code of chromium as follows:


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);
}

This function loads these pictures, and on the java side,


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;
}

In C + +, the function getHandleBitmap in java will be called. This function loads picture resources from jdk through context. getTheme (). obtainStyledAttributes. When displaying, image information is obtained through GetBitmap function, and layer_- > SetBitmap (bitmap) sets what is displayed with the following functions:


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_;
}

After this analysis, it seems impossible to solve this problem from the display. The only way is to replace the picture resources, which are in the android. jar package. Is there any other way? Analyze the source code,


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);
}

There are several images id information, can you overload it, so add your own


<?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>

Replace system resources and add android: theme= "@ style/MyTheme" your own theme style, problem solving


Related articles: