Android login interface implementation-supports clear input box and vibration effect
Demo Effect
The main code is as follows:
A custom EditText, used to display clear buttons when there is text:
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. animation. animation; import android. view. animation. cycleInterpolator; import android. view. animation. translateAnimation; import android. widget. editText; public cl Ass ClearEditText extends EditText implements View. onFocusChangeListener, TextWatcher {// reference of the delete button private Drawable mClearDrawable; // check whether the control has focus private boolean hasFoucs; public ClearEditText (Context context) {this (context, null );} public ClearEditText (Context context, AttributeSet attrs) {// The constructor is also very important here. Without this attribute, you cannot define this (context, attrs, android. r. attr. editTextStyle);} public ClearEditText (Cont Ext context, AttributeSet attrs, int defStyle) {super (context, attrs, defStyle); init ();} private void init () {// obtain the DrawableRight of EditText, if this parameter is not set, use the default image mClearDrawable = getCompoundDrawables () [2]; if (mClearDrawable = null) {// throw new NullPointerException ("You can add drawableRight attribute in XML"); mClearDrawable = getResources (). getDrawable (R. drawable. selector_ic_delete);} // getIntr InsicWidth () is the width of Drawable on the mobile phone. Therefore, different values are obtained at different resolutions. The key is mClearDrawable. setBounds (0, 0, mClearDrawable. getIntrinsicWidth (), mClearDrawable. getIntrinsicHeight (); // by default, the hidden icon setClearIconVisible (false) is set. // setOnFocusChangeListener (this) is used to set a listener with a changed focus ); // set the listening addTextChangedListener (this);}/*** in the input box because we cannot directly set the click event for EditText, so we use the place where we press it to simulate the Click Event * When we press the position in the EditText width-the distance from the icon to the right of the control-the width of the icon and * Edi Width of tText-even if the distance between the icon and the right of the control is clicked, the vertical direction is not considered */@ Override 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);}/*** when the ClearEditText focus changes, determine the display and hide of the clear icon in the string length settings */@ Override public void onFocusChange (View v, boolean hasFocus) {this. hasFoucs = hasFocus; if (hasFocus) {setClearIconVisible (getT Ext (). length ()> 0) ;}else {setClearIconVisible (false) ;}}/*** sets the display and hide of the clear icon, call setCompoundDrawables to plot it for EditText * @ param visible */protected void setClearIconVisible (boolean visible) {Drawable right = visible? MClearDrawable: null; setCompoundDrawables (getCompoundDrawables () [0], getCompoundDrawables () [1], right, getCompoundDrawables () [3]);} /*** callback method when the content in the input box changes */@ 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) {}/*** set shaking animation */public void setShakeAnimation () {this. setAnimation (shakeAnimation (5);}/*** shake Animation * @ param counts 1 second shake how many times * @ 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 ;}}
MainActivity. java mainly refers to the Click Event of a button in a pop-up sentence.
Import android. OS. bundle; import android. view. view; import android. widget. button; import android. widget. toast; import android. app. activity; public class MainActivity extends Activity {private Button btnLogin; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_login); btnLogin = (Button) this. findViewById (R. id. btnLogin);} public void login (View view) {Toast. makeText (this, "login", Toast. LENGTH_LONG ). show ();}}
The layout file is as follows:
There are also some selector files and image resources:
Bg_btn_style_green.xml
Bg_edittext_selector.xml
Free projects:
Http://yunpan.cn/cwWymCEMmgTzp (extract code: 317d)