Android AutoCompleteTextView supports mobile phone number formatting, with the history clearing operation, autocomplete clearing data

Source: Internet
Author: User

Android AutoCompleteTextView supports mobile phone number formatting, with the history clearing operation, autocomplete clearing data

One of my friends has encountered this problem: AutoCompleteTextView Enables automatic filling. At the same time, you must have the mobile phone Formatting Function. The last row in the drop-down list has a history clearing function. However, you can click "Clear history" to add the text to AutoCompleteTextView. This effect is obviously bad. So I wrote such a simple demo. To solve this problem. Let's talk about Code directly.

  The layout file (activity_main.xml) code 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: orientation = "vertical">

<TextView
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "Please input:"/>

<AutoCompleteTextView
Android: id = "@ + id/actv"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"/>

</LinearLayout>

  The code for the java file MainActivity. java is as follows:

  

Import android. app. Activity;
Import android. OS. Bundle;
Import android. text. Editable;
Import android. text. Selection;
Import android. text. TextWatcher;
Import android. view. View;
Import android. widget. AdapterView;
Import android. widget. AdapterView. OnItemClickListener;
Import android. widget. ArrayAdapter;
Import android. widget. AutoCompleteTextView;
Import android. widget. EditText;
Import android. widget. TextView;
Import android. widget. Toast;

Public class MainActivity extends Activity {

Private AutoCompleteTextView mAutoCompleteTextView;
Private String [] mAutoStrs = new String [] {"138 0013 8000", "13800138001 ",
"13800138002", "13800138003", "13800138004", "138 0013 clearing records "};
Private String mBeforeTextChangedStr = "";

Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
MAutoCompleteTextView = (AutoCompleteTextView) findViewById (R. id. actv );
ArrayAdapter <String> _ arrayAdapter = new ArrayAdapter <String> (this,
Android. R. layout. simple_dropdown_item_1line, mAutoStrs );
MAutoCompleteTextView. setAdapter (_ arrayAdapter );
MAutoCompleteTextView. setThreshold (1); // you will be prompted when you enter one character.
MAutoCompleteTextView. setOnItemClickListener (new OnItemClickListener (){
@ Override
Public void onItemClick (AdapterView <?> Arg0, View arg1, int arg2,
Long arg3 ){
String _ clearStr = "";
If (arg1 instanceof TextView ){
_ ClearStr = (TextView) arg1). getText (). toString ();
}
If (_ clearStr. equals ("138 0013 800 clear records ")){
MAutoCompleteTextView. setText (mBeforeTextChangedStr );
Editable _ editable = mAutoCompleteTextView. getText ();
Selection. setSelection (_ editable, _ editable. length ());
Toast. makeText (MainActivity. this, "cleared successfully! ",
Toast. LENGTH_LONG). show ();
}
}
});

PhoneNumAddSpaceOne (mAutoCompleteTextView );
}

/**
* Mobile phone number formatting code
*
* @ Param editText
* EditText object
*/
Public void phoneNumAddSpaceOne (final EditText editText ){
EditText. addTextChangedListener (new TextWatcher (){
Private int start;
Private int before;
Private StringBuilder stringBuilder;

@ Override
Public void onTextChanged (CharSequence s, int start, int before,
Int count ){
This. start = start;
This. before = before;
}

@ Override
Public void beforeTextChanged (CharSequence s, int start, int count,
Int after ){
}

@ Override
Public void afterTextChanged (Editable s ){
String _ str = s. toString ();
If (! IsNumeric (_ str. replace ("",""))){
Return;
}
MBeforeTextChangedStr = _ str;
// Format the mobile phone number xxx xxxx
If (s = null | s. length () = 0)
Return;
If (stringBuilder = null ){
StringBuilder = new StringBuilder ();
} Else {
StringBuilder. delete (0, stringBuilder. length ());
}
For (int I = 0; I <s. length (); I ++ ){
If (I! = 3 & I! = 8 & s. charAt (I) = ''){
Continue;
} Else {
StringBuilder. append (s. charAt (I ));
If (stringBuilder. length () = 4 | stringBuilder
. Length () = 9) & stringBuilder. charAt (stringBuilder. length ()-1 )! = ''){
StringBuilder. insert (stringBuilder. length ()-1 ,'');
}
}
}
If (! StringBuilder. toString (). equals (s. toString ())){
Int index = start + 1;
If (stringBuilder. charAt (start) = ''){
If (before = 0 ){
Index ++;
} Else {
Index --;
}
} Else {
If (before = 1 ){
Index --;
}
}
EditText. setText (stringBuilder. toString ());
EditText. setSelection (index );
}
}
});
}

/**
* Determines whether the string is a number.
*
* @ Param str
* String to be judged
* @ Return
*/
Public boolean isNumeric (String str ){
For (int I = str. length (); -- I> = 0 ;){
Int chr = str. charAt (I );
If (chr <48 | chr> 57)
Return false;
}
Return true;
}

}

  

 

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.