Android Login interface to clear the contents of the input box and vibration effect _android

Source: Internet
Author: User

This article for everyone to share the Android landing interface to clear the contents of the input box and vibration effect of all the code, the details are as follows:

Effect Chart:

The main code is as follows

A custom edittext that displays a clear button when text is available:

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 class Clearedittext extends EditText implements view.onfocuschangelistener,textwatcher{//delete button reference private Dra

  Wable mcleardrawable;

  Control has focus private Boolean hasfoucs;
  Public Clearedittext {This (context, NULL); "Public clearedittext" (context context, AttributeSet attrs) {//It is also important to construct the method, without which many attributes cannot be defined in the XML. TRS, Android.
  R.attr.edittextstyle);
    Public Clearedittext (context, AttributeSet attrs, int defstyle) {Super (context, attrs, Defstyle);
  Init (); private void Init () {//Get edittext DRAWableright, if there is no setting we will use the default picture mcleardrawable = Getcompounddrawables () [2];
        if (mcleardrawable = = null) {//throw new NullPointerException ("You can add drawableright attributes in XML");
      mcleardrawable = Getresources (). getdrawable (R.drawable.selector_ic_delete); //getintrinsicwidth () Gets the drawable width on the phone, so the values obtained at different resolutions are different, and the key location mcleardrawable.setbounds (0, 0, Mcleardrawa

      Ble.getintrinsicwidth (), Mcleardrawable.getintrinsicheight ());
      The default setting hides the icon setcleariconvisible (false);
      Set focus-Changing listening setonfocuschangelistener (this);
    Set the input box to change the content of the listening addtextchangedlistener (this);
     /** * Because we can not set the Click event directly to EditText, so we use to remember the position we pressed to simulate the click event * when we press the position at the edittext width-the icon to the right of the control spacing-the width of the icon and 
      * EditText Width-The distance between the icon and the right side of the control if we click on the icon, 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.ge

          TX () < ((GetWidth ()-getpaddingright ()));
          if (touchable) {This.settext ("");
    }} return Super.ontouchevent (event); /** * When Clearedittext focus changes, judge inside string length set clear icon display and hide */@Override public void Onfocuschange (View V
      , Boolean hasfocus) {this.hasfoucs = Hasfocus;
      if (hasfocus) {setcleariconvisible (GetText (). Length () > 0);
      else {setcleariconvisible (false);  }/** * Set clear icon display and hide, call setcompounddrawables for EditText draw up * @param visible/protected void
      Setcleariconvisible (Boolean visible) {drawable right = visible? Mcleardrawable:null;
    Setcompounddrawables (Getcompounddrawables () [0], Getcompounddrawables () [1], right, Getcompounddrawables () [3]); /** * The method of callback when the contents of the input box are changed
     * * @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) {} @Ov Erride public void aftertextchanged (Editable s) {}/** * Set shaking animation/public void Setshakeanima
    tion () {this.setanimation (Shakeanimation (5));  /** * Shaking animation * @param counts 1 seconds to shake how many under * @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 is mainly a pop-up word to indicate the click of the button event

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, "Landing", Toast.length_long). Show ();

  }

The layout files are 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 "and roid:orientation= "vertical" android:padding= "4.0dip" > <com.xuliugen.clearedittext.clearedittext android:id = "@+id/etxtemail" style= "@style/edittext" android:layout_width= "fill_parent" android:layout_height= "Wrap_conte NT "android:layout_margintop=" 30.0dip "android:drawableleft=" "@drawable/icon_reg_name" android:drawablepadding=
    "10.0dip" android:hint= "Use mailbox Login"/> <com.xuliugen.clearedittext.clearedittext android:id= "@+id/etxtpwd" style= "@style/edittext" android:layout_width= "fill_parent" android:layout_height= "Wrap_content" Android:layo ut_margintop= "20.0dip" android:drawableleft= "@drawable/icon_reg_password" android:drawablepadding= "10.0dip" an droid:hint= "Enter login password" android:iNputtype= "Textpassword"/> <button android:id= "@+id/btnlogin" style= "@style/biggreenbutton" android:l Ayout_width= "Fill_parent" android:layout_height= "wrap_content" android:layout_margintop= "20.0dip" android:onCl
 ick= "Login" android:text= "Landing"/> </LinearLayout>

In addition, there are 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>

This is the entire content of this article, I hope to learn more about Android software programming help.

Related Article

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.