格式化分隔手机号码

效果图:

格式化分隔手机号码_第1张图片

主布局:



    
    
    

activity:

package com.example.demo;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;

public class EdittextDemoActivity extends Activity implements OnCheckedChangeListener {
    private EditText et;
    
    private EditText etPasswordTxtTest;
    private CheckBox cbPasswordTxtTest;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_edittext_demo);
		et = (EditText) findViewById(R.id.et);
		et.addTextChangedListener(new MyTextWatcher(et));
		
		etPasswordTxtTest = (EditText) findViewById(R.id.et_password_txt_test);
		cbPasswordTxtTest = (CheckBox) findViewById(R.id.cb_password_txt_test);
		cbPasswordTxtTest.setOnCheckedChangeListener(this);
	}
	public void setUp(View v){
		et.setText("12345678901");
	}  
	
    public static class MyTextWatcher implements TextWatcher {  
        
        private EditText phoneNumberEditText;   
        String  tmp ; 
  
        int start,count,beforeSelectionStart; 
        public MyTextWatcher(EditText numberEditText) {  
            this.phoneNumberEditText = numberEditText;  
        }  
  
        @Override  
        public void onTextChanged(CharSequence s, int start, int before,int count) {  
        	System.out.println("onTextChanged ---- start = "+start+" before = "+before+" count = "+count+"  s= "+s);
        	this.start = start;
        	this.count = count;
        }  
  
        @Override  
        public void beforeTextChanged(CharSequence s, int start, int count,int after) { 
        	beforeSelectionStart = phoneNumberEditText.getSelectionStart();
        	System.out.println("beforeTextChanged getSelectionStart="+phoneNumberEditText.getSelectionStart());
        	System.out.println("beforeTextChanged ---- start = "+start+" count = "+count + " after = "+after+"  s= "+s);
        }  
  
        @Override  
        public void afterTextChanged(Editable s) {     
        	System.out.println("---afterTextChanged---");
        	
        	char c = '-';//' ';
   		    String sourceStr = s.toString();
			if(tmp != null && tmp.equals(sourceStr )){ 
   		    	return;
   		    } 
			tmp = separateString(sourceStr, 3, 3, c); 
			if(!tmp.equals(sourceStr) ){
				phoneNumberEditText.setText(tmp);  
				System.out.println("start = "+start + " s.length = "+s.length()+" count = "+count);
				if(start == 0 ){
					if(s.length() == ( count-1)){
						phoneNumberEditText.setSelection(tmp.length());
					}else if (s.length() ==  count) {
						phoneNumberEditText.setSelection(beforeSelectionStart);
					}
					
				}
				//phoneNumberEditText.setSelection(tmp.length());  
			}   
        }
        /**
         * 用指定字符分隔格式化字符串
         * 
(如电话号为1391235678 指定startIndex为3,step为4,separator为'-'经过此处理后的结果为
139-1234-5678) * @param source 需要分隔的字符串 * @param startIndex 开始分隔的索引号 * @param step 步长 * @param separator 指定的分隔符 * @return 返回分隔格式化处理后的结果字符串 */ private String separateString(String source, int startIndex, int step,char separator) { int times = 0; StringBuilder tmpBuilder = new StringBuilder(source); for(int i = 0;i

你可能感兴趣的:(控件)