国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

帶清除內容的EditText,支持運單格式的輸入模式

2019-11-09 15:36:56
字體:
來源:轉載
供稿:網友
package com.sfnp.warehouse.widget;import android.content.Context;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.view.View.OnFocusChangeListener;import android.view.animation.Animation;import android.view.animation.CycleInterpolator;import android.view.animation.TranslateAnimation;import android.widget.EditText;import com.sfnp.warehouse.R;public class ClearEditText extends EditText implements OnFocusChangeListener, TextWatcher { /** * 刪除按鈕的引用 */ PRivate Drawable mClearDrawable; /** * 是否是運單模式 */ private boolean isWaybillMode; //是否改變 private boolean isChanged; public ClearEditText(Context context) { this(context, null); } public ClearEditText(Context context, AttributeSet attrs) { //這里構造方法也很重要,不加這個很多屬性不能再xml里面定義 this(context, attrs, android.R.attr.editTextStyle); } public ClearEditText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(context); } private void init(Context context) { //獲取EditText的DrawableRight,假如沒有設置我們就使用默認的圖片 mClearDrawable = getCompoundDrawables()[2]; if (mClearDrawable == null) { mClearDrawable = getResources().getDrawable(R.drawable.ic_edit_clear); } mClearDrawable.setBounds(0, 0, mClearDrawable.getIntrinsicWidth(), mClearDrawable.getIntrinsicHeight()); setClearIconVisible(false); setOnFocusChangeListener(this); addTextChangedListener(this); initWaybillMode(); } private void initWaybillMode() { addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { if (isWaybillMode) { if (isChanged) { isChanged = false; return; } isChanged = true; String text = s.toString().replace(" ", ""); StringBuffer sb = new StringBuffer(); int index = 0; while ((index + 3) < text.length()) { sb.append(text.substring(index, index + 3) + " "); index += 3; } sb.append(text.substring(index)); text = sb.toString(); setText(text); setSelection(text.length()); } } @Override public void afterTextChanged(Editable s) { } }); } /** * 因為我們不能直接給EditText設置點擊事件,所以我們用記住我們按下的位置來模擬點擊事件 * 當我們按下的位置 在 EditText的寬度 - 圖標到控件右邊的間距 - 圖標的寬度 和 * EditText的寬度 - 圖標到控件右邊的間距之間我們就算點擊了圖標,豎直方向沒有考慮 */ @Override public boolean onTouchEvent(MotionEvent event) { if (getCompoundDrawables()[2] != null) { if (event.getAction() == MotionEvent.ACTION_UP) { boolean touchable = event.getX() > (getWidth() - getPaddingRight() - mClearDrawable.getIntrinsicWidth()) && (event.getX() < ((getWidth() - getPaddingRight()))); if (touchable) { this.setText(""); } } } return super.onTouchEvent(event); } /** * 當ClearEditText焦點發生變化的時候,判斷里面字符串長度設置清除圖標的顯示與隱藏 */ @Override public void onFocusChange(View v, boolean hasFocus) { if (hasFocus) { setClearIconVisible(getText().length() > 0); } else { setClearIconVisible(false); } } /** * 設置清除圖標的顯示與隱藏,調用setCompoundDrawables為EditText繪制上去 * * @param visible */ protected void setClearIconVisible(boolean visible) { Drawable right; if (visible) { right = mClearDrawable; } else { right = null; } setCompoundDrawables(getCompoundDrawables()[0], getCompoundDrawables()[1], right, getCompoundDrawables()[3]); } /** * 當輸入框里面內容發生變化的時候回調的方法 * 注意:setText()方法,無限遞歸調用導致棧溢出StackOverflowError */ @Override public void onTextChanged(CharSequence s, int start, int before, int count) { if (hasFocus()) { 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)); } /** * 晃動動畫 * * @param counts 1秒鐘晃動多少下 * @return */ public static Animation shakeAnimation(int counts) { Animation translateAnimation = new TranslateAnimation(0, 10, 0, 0); translateAnimation.setInterpolator(new CycleInterpolator(counts)); translateAnimation.setDuration(1000); return translateAnimation; } /** * 是否是運單模式 * * @param isWaybillMode */ public void setWaybillMode(boolean isWaybillMode) { this.isWaybillMode = isWaybillMode; }}

注意:運單模式輸入下,如果EditText設置了setMaxLength長度,那么這個長度要剛好等于運單號的長度加上空格的長度,否則可能出現下標越界的異常


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 泰和县| 岢岚县| 涡阳县| 蒙自县| 腾冲县| 运城市| 和田县| 湖口县| 从江县| 平舆县| 历史| 定结县| 鄂尔多斯市| 黄山市| 安远县| 周至县| 哈尔滨市| 漯河市| 包头市| 鄯善县| 鹰潭市| 咸宁市| 陇南市| 乌兰察布市| 电白县| 裕民县| 镇远县| 中方县| 六盘水市| 甘孜| 邵阳县| 邛崃市| 雷山县| 莆田市| 杭锦后旗| 黄骅市| 威海市| 金昌市| 个旧市| 拉萨市| 鲜城|