http://qq361119908.javaeye.com/blog/644891 package com.jp; import java.security.PublicKey; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.security.auth.PrivateCredentialPermission; import android.R.integer; import android.app.Activity; import android.content.DialogInterface; import android.os.Bundle; import android.provider.Contacts.People.Phones; import android.text.Editable; import android.text.Selection; import android.text.TextWatcher; import android.view.KeyEvent; import android.view.View; import android.view.View.OnKeyListener; import android.widget.EditText; import android.widget.Toast; public class EditTextTest extends Activity implements TextWatcher,OnKeyListener{ /** Called when the activity is first created. */ private EditText eText; private EditText eText2; List<String> phoneNums=new ArrayList<String>(); Map<String, String> map=new HashMap<String, String>(); String[] phoneDis=new String[]{"zhangsan"}; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); phoneNums.add("4325"); phoneNums.add("6546"); phoneNums.add("58678"); phoneNums.add("987978"); phoneNums.add("23254"); eText= (EditText) findViewById(R.id.EditText01); eText2= (EditText) findViewById(R.id.EditText02); eText.setText("14141234234,张三<4325>,14124141414,李四<6546>,王五<58678>,赵丽<987978>,哈哈<23254>,"); eText2.setText(phoneNums.toString().substring(1, phoneNums.toString().length()-1)); Editable etext = eText.getText(); int position = etext.length(); Selection.setSelection(etext, position); // Selection.setSelection(etext, position-2, position); // eText.setFocusable(false); //设置控件是否光标可见 eText.setOnKeyListener(this); //eText.setOnEditorActionListener(l) } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { // TODO Auto-generated method stub this.setTitle("-------"); return super.onKeyDown(keyCode, event); } @Override public void afterTextChanged(Editable s) { // TODO Auto-generated method stub } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { // TODO Auto-generated method stub } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { // TODO Auto-generated method stub } private boolean t1onkey(int keyCode) { // TODO Auto-generated method stub switch (keyCode) { case 67: try { int cInteger=eText.getSelectionEnd()-eText.getSelectionStart(); if (cInteger>0) { try { String esString=eText.getText().toString().trim(); for (String i : phoneNums) { String bbString= esString.substring(eText.getSelectionStart(), eText.getSelectionEnd()); int indexa1=bbString.indexOf("<"); int indexa2=bbString.indexOf(">"); String cc = bbString.substring(indexa1+1, indexa2); if (cc.equals(i)) { phoneNums.remove(i); eText2.setText(phoneNums.toString().substring(1, phoneNums.toString().length()-1)); } } } catch (Exception e) { // TODO Auto-generated catch block } return false; }else { String esString=eText.getText().toString().trim(); String aString=esString.substring(eText.getSelectionStart()-1, eText.getSelectionStart()); if (aString.equals(",")) { String beforCurrsString=esString.substring(0, eText.getSelectionStart()-1); int indexmin=beforCurrsString.lastIndexOf(","); Selection.setSelection(eText.getText(), indexmin+1,eText.getSelectionStart()); return true; } return false; } } catch (Exception e) { // TODO Auto-generated catch block return true; } case 21: // String esString=eText.getText().toString().trim(); // String[] aa = esString.split(","); // for (String string : aa) { // // try { // Long.parseLong(string+""); // } catch (Exception e) { // // try { // String beforCurrsString=esString.substring(0, eText.getSelectionStart()-1); // int indexmin1=beforCurrsString.lastIndexOf(","); // // int indexmin2=esString.indexOf(",", indexmin1); // // Selection.setSelection(eText.getText(), indexmin1+1,indexmin2); // return true; // } catch (Exception e1) { // // TODO Auto-generated catch block // // } // } // } // break; return false; } return false; } @Override public boolean onKey(View v, int keyCode, KeyEvent event) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.EditText01: return t1onkey(keyCode); // try { // String esString=eText.getText().toString().trim(); // String aString=esString.substring(eText.getSelectionStart()-1, eText.getSelectionStart()); // String beforCurrsString=esString.substring(0, eText.getSelectionStart()-1); // int indexmin1=beforCurrsString.lastIndexOf(","); // if (indexmin1<eText.getSelectionStart()) { // // } // // if (aString.equals(",")) { // String beforCurrsString=esString.substring(0, eText.getSelectionStart()-1); // // Selection.setSelection(eText.getText(), indexmin+1,eText.getSelectionStart()); // // return true; // } // return false; // // } catch (Exception e) { // // TODO Auto-generated catch block // // } } // EditTextTest.this.setTitle(eText.getSelectionStart()+"//"); return false; } }