Android Landing interface Implementation-support input box clear and vibration effect function

Source: Internet
Author: User

Demo effect

The main code is as follows

A custom edittext that enables you to display a clear button when you have text:

ImportAndroid.content.Context;Importandroid.graphics.drawable.Drawable;Importandroid.text.Editable;ImportAndroid.text.TextWatcher;ImportAndroid.util.AttributeSet;ImportAndroid.view.MotionEvent;ImportAndroid.view.View;ImportAndroid.view.animation.Animation;ImportAndroid.view.animation.CycleInterpolator;ImportAndroid.view.animation.TranslateAnimation;ImportAndroid.widget.EditText; Public  class clearedittext extends EditText implements View. Onfocuschangelistener,textwatcher{     //Delete a reference to a button    PrivateDrawable mcleardrawable;//Whether the control has focus    Private BooleanHasfoucs; Public Clearedittext(Context context) { This(Context,NULL); } Public Clearedittext(context context, AttributeSet attrs) {//Here The construction method is also very important, do not add this many attributes can no longer be defined in XML         This(Context, Attrs, Android.)    R.attr.edittextstyle); } Public Clearedittext(context context, AttributeSet attrs,intDefstyle) {Super(Context, attrs, Defstyle);    Init (); }Private void Init() {//Get EditText's drawableright, if not set we'll use the default picturemcleardrawable = Getcompounddrawables () [2];if(Mcleardrawable = =NULL) {//throw new NullPointerException ("You can add Drawableright attribute in XML");mcleardrawable = Getresources (). getdrawable (R.drawable.selector_ic_delete); }//getintrinsicwidth () is the width of the drawable on the phone, so the value obtained at different resolutions is different, the key is whereMcleardrawable.setbounds (0,0, Mcleardrawable.getintrinsicwidth (), Mcleardrawable.getintrinsicheight ());//Default settings Hide iconSetcleariconvisible (false);//Set focus to change the monitorSetonfocuschangelistener ( This);//Set the monitor to change the contents of the input boxAddtextchangedlistener ( This); }/** * Because we can't set the click event directly to EditText, so we use the location we pressed to simulate the click event * when we press the position in the width of the EditText-icon to the right of the control spacing-the width of the icon and * EditText Width-icon to the right of the control between the spacing we even click on the icon, vertical direction is not considered * /        @Override         Public Boolean ontouchevent(Motionevent event) {if(event.getaction () = = motionevent.action_up) {if(Getcompounddrawables () [2] !=NULL) {Booleantouchable = Event.getx () > (GetWidth ()-gettotalpaddingright ()) && (Event.getx () &L T (GetWidth ()-getpaddingright ()));if(touchable) { This. SetText (""); }                }            }return Super. Ontouchevent (event); }/** * When the clearedittext focus changes, determine the length of the string to set the clear icon display and hide */        @Override         Public void Onfocuschange(View V,BooleanHasfocus) { This. Hasfoucs = Hasfocus;if(Hasfocus) {setcleariconvisible (GetText (). Length () >0); }Else{setcleariconvisible (false); }        }/** * Set clear icon display and hide, call Setcompounddrawables to EditText draw up * @param visible */        protected void setcleariconvisible(BooleanVisible) {drawable right = visible? Mcleardrawable:NULL; Setcompounddrawables (Getcompounddrawables () [0], Getcompounddrawables () [1], right, getcompounddrawables () [3]); }/** * The callback method when the contents of the input box are changed */        @Override         Public void ontextchanged(Charsequence S,intStartintCountintAfter) {if(Hasfoucs) {setcleariconvisible (s.length () >0); }        }@Override         Public void beforetextchanged(Charsequence S,intStartintCountintAfter) {}@Override         Public void aftertextchanged(Editable s) {        }/** * Set shaking animation * /         Public void setshakeanimation(){ This. Setanimation (Shakeanimation (5)); }/** * Shaking animation * @param counts 1 seconds shaking how many next * @return * *         Public StaticAnimationshakeanimation(intCounts) {Animation translateanimation =NewTranslateanimation (0,Ten,0,0); Translateanimation.setinterpolator (NewCycleinterpolator (counts)); Translateanimation.setduration ( +);returnTranslateanimation; }}

Mainactivity.java is mainly a pop-up sentence that indicates a button click event

ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.widget.Button;ImportAndroid.widget.Toast;Importandroid.app.Activity; Public  class mainactivity extends Activity {    PrivateButton 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:

<?xml version= "1.0" encoding= "Utf-8"?><linearlayout  xmlns: Android  = "http://schemas.android.com/apk/res/android"  android:layout_width  =" fill_parent " android:layout_height  =" fill_parent " android:background  =" #ffffffff " android:orientation  = "vertical"  android:padding  =" 4.0dip ";     <com.xuliugen.clearedittext.ClearEditTextandroid:id="@+id/etxtemail"  Style="@style/edittext"android:layout_width="Fill_parent"android:layout_ Height="Wrap_content"android:layout_margintop="30.0dip"android: Drawableleft= "@drawable/icon_reg_name"android:drawablepadding="10.0dip" android:hint="Login with Mailbox" />                                                                    <Com.xuliugen.clearedittext.ClearEditText        Android:id="@+id/etxtpwd"        style="@style/edittext"        Android:layout_width="Fill_parent"        Android:layout_height="Wrap_content"        Android:layout_margintop="20.0dip"        Android:drawableleft="@drawable/icon_reg_password"        android:drawablepadding="10.0dip"        Android:hint="Enter login password"        Android:inputtype="Textpassword"/>    <buttonandroid:id="@+id/btnlogin"style="@style/biggreenbutton"  android:layout_width="Fill_parent"android:layout_height="Wrap_content"  android:layout_margintop="20.0dip"android:onclick="Login" Android:text="Login" />                                                        </linearlayout>

There are also some selector files, picture resources, etc.:
Bg_btn_style_green.xml

<?xml version= "1.0" encoding= "Utf-8"?><selector xmlns:android="Http://schemas.android.com/apk/res/android">    <item android:state_enabled="false"><shape android:shape="Rectangle">            <corners Android:radius="2.0dip" />            <solid android:color="@color/green_btn_color_disable" />        </shape></Item>    <item android:state_pressed="true"><shape android:shape="Rectangle">            <corners Android:radius="2.0dip" />            <solid android:color="@color/green_btn_color_pressed" />        </shape></Item>    <item><shape android:shape="Rectangle">            <corners Android:radius="2.0dip" />            <solid android:color="@color/green_btn_color_normal" />        </shape></Item></selector>

Bg_edittext_selector.xml

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <item android:drawable="@drawable/input_bar_bg_active" android:state_focused="true"/>    <item android:drawable="@drawable/input_bar_bg_normal"/></selector>

Project Free:
HTTP://YUNPAN.CN/CWWYMCEMMGTZP (Extract code: 317d)

Android Landing Interface implementation-support the input box clear and vibrate effect function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.