Android開發之WebView輸入框提示解決辦法_Android

來源:互聯網
上載者:User

做基於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" 自己的主題風格,問題解決

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.