android - TextWatcher (onTextChanged method) -


i have question textwatcher.i have editbox (input type phone)

what want do: when app opened, format shown in edittext this.

http://hizliresim.com/vndxp4

and when user enters numbers,under-bar should deleted automatically.

when run project,i can show format in picture "before" "variable how use ontextchanged method process.do have idea?

my code:

  import android.support.v7.app.appcompatactivity;   import android.os.bundle;   import android.text.editable;   import android.text.textwatcher;   import android.widget.edittext;    import java.lang.reflect.array;   import java.util.arrays;   public class mainactivity extends appcompatactivity {     edittext edittext1;    string before ="_ _ _ - _ _ _ _ _ _ _";   @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);       edittext1=(edittext)findviewbyid(r.id.edittext1);     edittext1.settext(before);    edittext1.addtextchangedlistener(textwatcher);   }  textwatcher textwatcher=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)     {         //char single=0;      string character=edittext1.gettext().tostring();         //system.out.println(character);             delete();       }      @override     public void aftertextchanged(editable s)     {      } };  public void delete() {     for(int i=0; i<=before.length(); i=i +2)     {         before.substring(i);      }  } 

}

my xml file:

 <?xml version="1.0" encoding="utf-8"?>  <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" android:paddingleft="@dimen/activity_horizontal_margin"  android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" android:paddingbottom="@dimen/activity_vertical_margin" tools:context=".mainactivity">     <edittext     android:layout_width="350sp"     android:layout_height="wrap_content"     android:id="@+id/edittext1"     android:layout_margintop="150dp"     android:layout_centerhorizontal="true"     android:inputtype="phone"     android:textsize="40sp"     android:maxlength="24"      /> 

you can add hint text view when type hint gone. this: add textview android:hint="phone_number"


Comments