package com.example.traindemo.util; import java.util.Calendar; import com.example.traindemo.R; import android.app.DatePickerDialog; import android.content.Context; import android.graphics.Rect; import android.graphics.drawable.Drawable; import android.util.AttributeSet; import android.util.Log; import android.view.MotionEvent; import android.widget.DatePicker; import android.widget.EditText; /** * @2014-12-09 10:58:24 * @author lxg * */ public class EditTextWithDate extends EditText { private final static String TAG = "EditTextWithDate"; private Drawable dateIcon; private Context mContext; private Calendar c; private int mYear; private int mMonth; private int mDay; private boolean datePickerEnabled = true; public EditTextWithDate(Context context) { super(context); mContext = context; init(); } public EditTextWithDate(Context context, AttributeSet attrs) { super(context, attrs); mContext = context; init(); } public EditTextWithDate(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); mContext = context; init(); } private void init() { dateIcon = mContext.getResources().getDrawable(R.drawable.ic_launcher); dateIcon.setBounds(-11, 0, 70, 70); setCompoundDrawables(null, null, dateIcon, null); // setCompoundDrawablesWithIntrinsicBounds(null, null, dateIcon, null); c = Calendar.getInstance(); final Calendar cal = Calendar.getInstance(); mYear = cal.get(Calendar.YEAR); // 獲取當前年份 mMonth = cal.get(Calendar.MONTH);// 獲取當前月份 mDay = cal.get(Calendar.DAY_OF_MONTH);// 獲取當前月份的日期號碼 this.setEnabled(false); } @Override public boolean onTouchEvent(MotionEvent event) { if (dateIcon != null && event.getAction() == MotionEvent.ACTION_UP) { int eventX = (int) event.getRawX(); int eventY = (int) event.getRawY(); Log.e(TAG, "eventX = " + eventX + "; eventY = " + eventY); Rect rect = new Rect(); getGlobalVisibleRect(rect); rect.left = rect.right - 81; if (rect.contains(eventX, eventY)) { if (datePickerEnabled) { new DatePickerDialog(mContext, new DatePickerDialog.OnDateSetListener() { public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { mYear = year; mMonth = monthOfYear; mDay = dayOfMonth; setText(new StringBuilder().append("") .append(mYear).append("-") .append(mMonth + 1).append("-") .append(mDay).append("")); } }, c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH)).show(); } } } return super.onTouchEvent(event); } public boolean isDatePickerEnabled() { return datePickerEnabled; } public void setDatePickerEnabled(boolean datePickerEnabled) { this.datePickerEnabled = datePickerEnabled; if (!datePickerEnabled) { setText(""); } } }
自定義帶刪除功能的EditText package com.example.view; import com.example.ui.R; import android.content.Context; import android.graphics.Rect; import android.graphics.drawable.Drawable; import android.text.Editable; import android.text.TextWatcher; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; import android.widget.EditText; import android.widget.Toast; import android.view.View.OnFocusChangeListener;; public class EditTextWithDelete extends EditText implements OnFocusChangeListener{ private Drawable imgEnable; private Context context; public EditTextWithDelete(Context context) { super(context); this.context = context; init(); public EditTextWithDelete(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); this.context = context; init(); } public EditTextWithDelete(Context context, AttributeSet attrs) { super(context, attrs); this.context = context; init(); } private void init() { //獲取圖片資源 imgEnable = context.getResources().getDrawable(R.drawable.delete); 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) { } public void afterTextChanged(Editable s) { setDrawable(); Toast.makeText(context, getText(), 10).show(); } }); setDrawable(); } /** * 設置刪除圖片 */ private void setDrawable() { if(length() == 0) { setCompoundDrawablesWithIntrinsicBounds(null, null, null, null); }else { setCompoundDrawablesWithIntrinsicBounds(null, null, imgEnable, null); } } /** * event.getX() 獲取相對應自身左上角的X座標 * event.getY() 獲取相對應自身左上角的Y座標 * getWidth() 獲取控件的寬度 * getTotalPaddingRight() 獲取刪除圖標左邊緣到控件右邊緣的距離 * getPaddingRight() 獲取刪除圖標右邊緣到控件右邊緣的距離 * getWidth() - getTotalPaddingRight() 計算刪除圖標左邊緣到控件左邊緣的距離 * getWidth() - getPaddingRight() 計算刪除圖標右邊緣到控件左邊緣的距離 */ public boolean onTouchEvent(MotionEvent event) { if(imgEnable != null && event.getAction() == MotionEvent.ACTION_UP) { int x = (int) event.getX() ; //判斷觸摸點是否在水平範圍內 boolean isInnerWidth = (x > (getWidth() - getTotalPaddingRight())) && (x < (getWidth() - getPaddingRight())); //獲取刪除圖標的邊界,返回一個Rect對象 Rect rect = imgEnable.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) { setText(""); } } return super.onTouchEvent(event); } @Override protected void finalize() throws Throwable { super.finalize(); } @Override public void onFocusChange(View v, boolean hasFocus) { if(hasFocus) { setDrawable(); }else { setCompoundDrawablesWithIntrinsicBounds(null, null, null, null); } } }