基本信息
源码名称:android 模仿微博登陆框实例源码
源码大小:2.52M
文件格式:.rar
开发语言:Java
更新时间:2014-08-22
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍
当文字输入时改变图片  仿微博

package com.example.drawableedittext;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;

public class DrawableEditText extends RelativeLayout {

	private Context mContext;
	private EditText mEditText;
	private ImageView mImage;

	// 文本框不为空 图片资源
	private int imgRes;

	// 文本框为空 图片资源
	private int nullImgRes;

	public DrawableEditText(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.mContext = context;
		init();

		// 得到图片资源
		imgRes = attrs.getAttributeResourceValue(null, "imgRes", 0);
		nullImgRes = attrs.getAttributeResourceValue(null, "nullImgRes", 0);
		if (nullImgRes != 0) {
			setDrawable();
		}

		// 设置文字大小
		int textSize = attrs.getAttributeResourceValue(null, "textSize", 0);
		if (textSize != 0) {
			mEditText.setTextSize(textSize);
		}

		// 设置edittext的hint提示
		int hint = attrs.getAttributeResourceValue(null, "hint", 0);
		if (hint != 0) {
			mEditText.setHint(hint);
		}

		// 设置文本颜色
		int textColor = attrs.getAttributeResourceValue(null, "textColor", 0);
		if (textColor != 0) {
			mEditText.setTextColor(textColor);
		}
	}

	// 初始化布局和控件
	public void init() {
		LayoutInflater inflater = LayoutInflater.from(mContext);
		View view = inflater.inflate(R.layout.drawable_edittext, this);
		mEditText = (EditText) view.findViewById(R.id.edittext);
		mImage = (ImageView) view.findViewById(R.id.image);
	}

	// 根据文本框是否为空设置不同的图片
	private void setDrawable() {
		if (mEditText.getText().toString().equals("")) {
			mImage.setImageResource(nullImgRes);
		} else {
			mImage.setImageResource(imgRes);
		}
	}

	@Override
	protected void onFinishInflate() {
		super.onFinishInflate();
		// 文本框的text改变监听
		mEditText.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
			}

			@Override
			public void afterTextChanged(Editable s) {
				setDrawable();
			}
		});
	}

	/**
	 * @description 输入框是否有文字
	 * @return
	 */
	public boolean isEmpty() {
		return getText() == null || getText().length() == 0 ? true : false;
	}

	/**
	 * @description 获取输入框内的文字
	 * @return
	 */
	public String getText() {
		return mEditText.getText().toString();
	}

	/**
	 * @description 设置输入框内的文字
	 * @return
	 */
	public void setText(String s) {
		mEditText.setText(s);
	}
}