Android 帶清除功能的輸入框控制元件ClearEditText
阿新 • • 發佈:2019-01-07
轉載地址。如有轉載請標明地址
demo下載:http://download.csdn.net/detail/day_moon/9664470
效果圖:
ClearEditText類的定義
public class ClearEditText extends EditText implements View.OnFocusChangeListener, TextWatcher { private Drawable mClearDrawable;//刪除按鈕 private boolean hasFoucs;//是否有焦點 public ClearEditText(Context context) { this(context, null); } public ClearEditText(Context context, AttributeSet attrs) { this(context, attrs, android.R.attr.editTextStyle); //不加該屬性,不能在配置檔案裡面定義 } public ClearEditText(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } private voidinit() { 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); } public boolean onTouchEvent(MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_UP) { if (getCompoundDrawables()[2] != null) { boolean touchable = event.getX() > (getWidth() - getTotalPaddingRight()) && (event.getX() < (getWidth() - getPaddingRight())); if (touchable) { 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); } } public void setClearIconVisible(boolean visible) { Drawable right = visible ? mClearDrawable : null; setCompoundDrawables(getCompoundDrawables()[0], getCompoundDrawables()[1], right, getCompoundDrawables()[3]); } public void beforeTextChanged(CharSequence s, int start, int count, int after) { } public void afterTextChanged(Editable s) { } /** * 當輸入框裡面內容發生變化的時候回撥的方法 */ @Override public void onTextChanged(CharSequence s, int start, int count, int after) { if (hasFoucs) { setClearIconVisible(s.length() > 0); } } public void setShakeAnimation() { this.setAnimation(shakeAnimation(5));//設定晃動動畫 } private static Animation shakeAnimation(int counts) { Animation translateAnimation = new TranslateAnimation(0, 10, 0, 0); translateAnimation.setInterpolator(new CycleInterpolator(counts)); translateAnimation.setDuration(1000); return translateAnimation; } }
MainActivity 主類:
public class MainActivity extends Activity { private Toast mToast; public Button bt_login; public ClearEditText username, password; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView() { username = (ClearEditText) findViewById(R.id.username); password = (ClearEditText) findViewById(R.id.password); bt_login = (Button) findViewById(R.id.login); bt_login.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { if (TextUtils.isEmpty(username.getText())){ username.setShakeAnimation(); Toast.makeText(MainActivity.this,"使用者名稱不能為空!",Toast.LENGTH_LONG).show(); return; } if(TextUtils.isEmpty(password.getText())){ password.setShakeAnimation(); Toast.makeText(MainActivity.this,"密碼不能為空!",Toast.LENGTH_LONG).show(); return; } } }); } }佈局檔案:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"> <view.ClearEditText android:id="@+id/username" android:layout_marginTop="60dp" android:layout_width="fill_parent" android:drawableLeft="@mipmap/icon_user" android:layout_marginLeft="10dip" android:layout_marginRight="10dip" android:singleLine="true" android:drawableRight="@drawable/delete_selector" android:hint="輸入使用者名稱" android:layout_height="wrap_content" > </view.ClearEditText> <view.ClearEditText android:id="@+id/password" android:layout_marginLeft="10dip" android:layout_marginRight="10dip" android:layout_marginTop="10dip" android:drawableLeft="@mipmap/account_icon" android:hint="輸入密碼" android:singleLine="true" android:password="true" android:drawableRight="@drawable/delete_selector" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/username"> </view.ClearEditText> <Button android:id="@+id/login" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="10dip" android:layout_marginRight="10dip" android:textSize="18sp" android:textColor="@android:color/white" android:layout_below="@+id/password" android:layout_marginTop="25dp" android:text="登入" /> </RelativeLayout>