[Android]給EditText添加圖文hint

來源:互聯網
上載者:User

[Android]給EditText添加圖文hint
原因:有時候我們需要當沒有文字的時候背景顯示一個圖文混合的背景提示,這時候如果採用控制項疊加的做法效率會很低,所以我們可以採用重載View的onDraw方法解決方案:
這個是效果,那個搜尋方塊。

package com.finals.teltem.view;import android.content.Context;import android.content.res.TypedArray;import android.graphics.Canvas;import android.graphics.Paint;import android.graphics.Paint.FontMetrics;import android.graphics.drawable.Drawable;import android.util.AttributeSet;import android.widget.EditText;import com.finals.teltem.R;public class SearchView extends EditText {float searchSize = 0;float textSize = 0;int textColor = 0xFF000000;Drawable mDrawable;Paint paint;public SearchView(Context context, AttributeSet attrs) {super(context, attrs);InitResource(context, attrs);InitPaint();}private void InitResource(Context context, AttributeSet attrs) {TypedArray mTypedArray = context.obtainStyledAttributes(attrs, R.styleable.searchedit);float density = context.getResources().getDisplayMetrics().density;searchSize = mTypedArray.getDimension(R.styleable.searchedit_imagewidth, 18 * density + 0.5F);textColor = mTypedArray.getColor(R.styleable.searchedit_textColor, 0xFF848484);textSize = mTypedArray.getDimension(R.styleable.searchedit_textSize, 14 * density + 0.5F);mTypedArray.recycle();}private void InitPaint() {paint = new Paint(Paint.ANTI_ALIAS_FLAG);paint.setColor(textColor);paint.setTextSize(textSize);}@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);DrawSearchIcon(canvas);}private void DrawSearchIcon(Canvas canvas) {if (this.getText().toString().length() == 0) {float textWidth = paint.measureText("搜尋");float textHeight = getFontLeading(paint);float dx = (getWidth() - searchSize - textWidth - 8) / 2;float dy = (getHeight() - searchSize) / 2;canvas.save();canvas.translate(getScrollX() + dx, getScrollY() + dy);if (mDrawable != null) {mDrawable.draw(canvas);}canvas.drawText("搜尋", getScrollX() + searchSize + 8, getScrollY() + (getHeight() - (getHeight() - textHeight) / 2) - paint.getFontMetrics().bottom - dy, paint);canvas.restore();}}@Overrideprotected void onAttachedToWindow() {super.onAttachedToWindow();if (mDrawable == null) {try {mDrawable = getContext().getResources().getDrawable(com.finals.teltem.R.drawable.serarch);mDrawable.setBounds(0, 0, (int) searchSize, (int) searchSize);} catch (Exception e) {}}}@Overrideprotected void onDetachedFromWindow() {if (mDrawable != null) {mDrawable.setCallback(null);mDrawable = null;}super.onDetachedFromWindow();}public float getFontLeading(Paint paint) {FontMetrics fm = paint.getFontMetrics();return fm.bottom - fm.top;}}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.