这是我前几天做的自定义控件实现的效果,其实这个图片也可以不用自定义效果,直接把三个控件组合在一起,这样的功能就能实现,但是我认为这个比较麻烦,可用性比较低,所以我自己研究了一个自定义控件结合EditText、TextView、ImageButton.首先看我写的第一种方法:
public class ClearEditText extends EditText implements
OnFocusChangeListener, TextWatcher {
private Drawable mClearDrawable;
private boolean hasFoucs;
private String text = "";
public ClearEditText(Context context) {
this(context, null);
}
public ClearEditText(Context context, AttributeSet attrs) {
this(context, attrs, android.R.attr.editTextStyle);
TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.ClearEditText);
String text1 = a.getString(R.styleable.ClearEditText_myText);
text = text1;
a.recycle();
}
public ClearEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.ClearEditText);
String text1 = a.getString(R.styleable.ClearEditText_myText);
text = text1;
a.recycle();
}
private void init() {
mClearDrawable = getCompoundDrawables()[2];
if (mClearDrawable == null) {
mClearDrawable = getResources().getDrawable(R.drawable.delete_selector);
}
mClearDrawable.setBounds(0, 0, mClearDrawable.getIntrinsicWidth(), mClearDrawable.getIntrinsicHeight());
setClearIconVisible(false);
setOnFocusChangeListener(this);
addTextChangedListener(this);
}
@SuppressLint("DrawAllocation")
@Override
protected void onDraw(Canvas canvas) {
Paint paint = new Paint();
paint.setTextSize(20);
paint.setColor(Color.GRAY);
canvas.drawText(text, 10, getHeight() / 2 + 5, paint);
super.onDraw(canvas);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
if (getCompoundDrawables()[2] != null) {
//判断触摸点是否在水平范围内
boolean isInnerWidth = event.getX() > (getWidth() - getTotalPaddingRight())
&& (event.getX() < ((getWidth() - getPaddingRight())));
//获取删除图标的边界,返回一个Rect对象
Rect rect = getCompoundDrawables()[2].getBounds();
//获取删除图标的高度
int height = rect.height();
int y = (int) event.getY();
//计算图标底部到控件底部的距离
int distance = (getHeight() - height) /2;
//判断触摸点是否在竖直范围内(可能会有点误差)
//触摸点的纵坐标在distance到(distance+图标自身的高度)之内,则视为点中删除图标
boolean isInnerHeight = (y > distance) && (y < (distance + height));
if(isInnerWidth && isInnerHeight) {
this.setText("");
}
}
}
return super.onTouchEvent(event);
}
@Override
public void onFocusChange(View v, boolean hasFocus) {
this.hasFoucs = hasFocus;
if (hasFocus) {
setClearIconVisible(getText().length() > 0);
} else {
setClearIconVisible(false);
}
}
protected void setClearIconVisible(boolean visible) {
Drawable right = visible ? mClearDrawable : null;
setCompoundDrawables(getCompoundDrawables()[0],
getCompoundDrawables()[1], right, getCompoundDrawables()[3]);
}
@Override
public void onTextChanged(CharSequence s, int start, int count,
int after) {
if(hasFoucs){
setClearIconVisible(s.length() > 0);
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
public void setShakeAnimation(){
this.setAnimation(shakeAnimation(5));
}
public static Animation shakeAnimation(int counts){
Animation translateAnimation = new TranslateAnimation(0, 10, 0, 0);
translateAnimation.setInterpolator(new CycleInterpolator(counts));
translateAnimation.setDuration(1000);
return translateAnimation;
}
}
这是array.xml配置文件
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="ClearEditText">
<attr name="myText" format="string" />
</declare-styleable>
</resources>
第二种方法比较复杂点:
edit_text_btn.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btn_et"
android:layout_alignParentLeft="true"
android:layout_marginLeft="75dp"
android:layout_alignParentTop="true"
android:textSize="22dp"
android:textColor="@color/black"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn_tv"
android:layout_alignParentLeft="true"
android:layout_alignTop="@+id/btn_et"
android:layout_alignBottom="@+id/btn_et"
android:textSize="22dp"
android:textColor="@color/white"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imag2"
android:layout_alignParentRight="true"
android:layout_alignBottom="@+id/btn_et"
android:layout_alignTop="@+id/btn_et"
android:background="@drawable/zhanka"/>
</RelativeLayout>
MyEditTextView.java 自定义类
public class MyEditTextView extends RelativeLayout {
private EditText et;
private TextView tv;
private ImageButton Ibt;
private Context context;
public MyEditTextView( Context context) {
super(context);
}
public MyEditTextView(Context context, AttributeSet attrs) {
super(context, attrs);
this.context=context;
LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.edit_text_btn,this);
et=(EditText)findViewById(R.id.btn_et);
tv=(TextView)findViewById(R.id.btn_tv);
Ibt=(ImageButton)findViewById(R.id.imag2);
}
/**
* 设置字体
* @param text
*/
public void setText(String text)
{
tv.setText(text);
}
public void changeActivity(final Context context,final String className)
{
Ibt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try {
Intent intent=new Intent(context,Class.forName(className));
context.startActivity(intent);
} catch (Exception e)
{
e.printStackTrace();
}
}
});
}
}
这是调用得类的方法
image.setText("单位信息");
department=(MyEditTextView)findViewById(R.id.image2);
department.changeActivity(PersonalInformationChangeActivity.this,
departmentActivity.class.getName() );
今天就写到这了,希望对大家有所帮助.