自訂帶刪除按鈕的EditText,按鈕edittext

來源:互聯網
上載者:User

自訂帶刪除按鈕的EditText,按鈕edittext

在做這個小的控制項的時候,其實我知道網上已經有很多了,但是還是想照自己的思路,親手寫下,功能很簡單;

就是

   (1)在EditText沒有輸入內容的時候隱藏刪除按鈕

(2 )在輸入內容的使用顯示刪除按鈕

(3 )在點擊刪除按鈕的時候  ,如果是點擊的話刪除一個字元,如果是長按的話按照一定的速度一個一個刪除,刪除每一個字元以後游標必須在最後

看下:

基本上就這幾點,實現起來很簡單,我沒有通過Extends EditText  來實現,使用的是布局。

        要注意的是 

(1)長按刪除的時候要在onLongClick中發送刪除的msg,然後開始刪除,知道按鈕的執行了onkeyUp的時候刪除msg,這個是通過Handler來實現的。

(2)imageButton必須要擷取到焦點,如果沒有焦點點擊沒有反應

        然後就是代碼了:

public class HuahuaDelectEdit extends  LinearLayout implements OnClickListener,OnLongClickListener,OnKeyListener{private Context mContext;private LayoutInflater mLayoutInflater;private EditText mEditText;private ImageButton mImageButton;private static final long DELECTPRETIME = 500;private AttributeSet mAttributeSet;private String mTitle;private Handler mHandler = new Handler(){public void handleMessage(android.os.Message msg) {switch (msg.what) {case 1:delect();mHandler.sendEmptyMessageDelayed(1, DELECTPRETIME);break;default:break;}};};public HuahuaDelectEdit(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);this.mAttributeSet =attrs;this.mContext = context;initView();}public HuahuaDelectEdit(Context context, AttributeSet attrs) {super(context, attrs);this.mAttributeSet =attrs;this.mContext = context;initView();}public HuahuaDelectEdit(Context context) {super(context);this.mContext = context;initView();}public void initView() {TypedArray a = mContext.obtainStyledAttributes(mAttributeSet, R.styleable.delectedit);mTitle = a.getString(R.styleable.delectedit_title);mLayoutInflater = LayoutInflater.from(mContext);View view = mLayoutInflater.inflate(R.layout.item_delect_edit, this);mEditText = (EditText)view.findViewById(R.id.et_text);mImageButton = (ImageButton)view.findViewById(R.id.ib_delect);setHint(mTitle);mEditText.addTextChangedListener(new TextWatcher() {@Overridepublic void onTextChanged(CharSequence s, int start, int before, int count) {}@Overridepublic void beforeTextChanged(CharSequence s, int start, int count,int after) {}@Overridepublic void afterTextChanged(Editable s) {setDelectShow();}});mImageButton.setOnClickListener(this);mImageButton.setOnLongClickListener(this);mImageButton.setOnKeyListener(this);}@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.ib_delect://mEditText.setText("");delect();break;default:break;}}public void setDelectShow(){if (mEditText.getText().toString().length() != 0) {mImageButton.setVisibility(View.VISIBLE);}else {mImageButton.setVisibility(View.GONE);}}@Overridepublic boolean onLongClick(View v) {Log.e("長點擊", "點擊點擊點擊");mHandler.sendEmptyMessage(1);return false;}public void delect(){if (mEditText.getText().toString().length() == 0) {mHandler.removeMessages(1);return;}mEditText.setText(mEditText.getText().subSequence(0, mEditText.getText().toString().length()-1));Editable etext = mEditText.getText();Selection.setSelection(etext, etext.length());}@Overridepublic boolean onKey(View v, int keyCode, KeyEvent event) {switch (keyCode) {case KeyEvent.ACTION_UP:mHandler.removeMessages(1);break;default:break;}return false;}public String getText(){return mEditText.getText().toString();}public void setHint(String hint){mEditText.setHint(hint);}}

Demo:http://download.csdn.net/detail/u012808234/9070161

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

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