码迷,mamicode.com
首页 > 移动开发 > 详细

[Android]给EditText添加图文hint

时间:2015-04-15 19:39:26      阅读:214      评论:0      收藏:0      [点我收藏+]

标签:

原因:

有时候我们需要当没有文字的时候背景显示一个图文混合的背景提示,这时候如果采用控件叠加的做法效率会很低,所以我们可以采用重载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);
	}

	@Override
	protected 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();
		}
	}

	@Override
	protected 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) {

			}
		}
	}

	@Override
	protected 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;
	}

}


[Android]给EditText添加图文hint

标签:

原文地址:http://blog.csdn.net/windowsxp2014/article/details/45062447

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!