http://www.2cto.com/kf/201402/280181.html



    public void hideKeyboard() {

        View focusView = this.getCurrentFocus();

        if (focusView == null || focusView.getTag() == null) {

            return;

        }

        try {

            InputMethodManager imm = ((InputMethodManager) this.getSystemService(Activity.INPUT_METHOD_SERVICE));

            imm.hideSoftInputFromWindow(focusView.getWindowToken(),   InputMethodManager.HIDE_NOT_ALWAYS);

            focusView.setTag(null);

        } catch (Exception e) {

            Log.e("SoftInput:Hiding had a wrong.", e.toString());

        }

    }


public class MainActivity extends Activity {
 
    private EditText edittext;
    private Button button;
     
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final View rootView = this.findViewById(R.id.rootview);
        edittext = (EditText) this.findViewById(R.id.edittext1);
        //限定只能输入数字
        edittext.setInputType(EditorInfo.TYPE_CLASS_NUMBER);
        //可以获取焦点
        button = (Button)this.findViewById(R.id.button1);
        button.setFocusable(true);
        button.setFocusableInTouchMode(true);
         
        // 空白处点击,隐藏软键盘
        rootView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                hideSoftInput();
            }
        });
 
        // 一旦获取焦点,设置光标位置
        edittext.setOnFocusChangeListener(new OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    String mobile = getMobile(edittext.getText().toString());
                    setCursorPosition(mobile.length());
                }
            }
        });
         
        // 返回true,手动处理touch事件,即使edittext获取了焦点,也不会自动弹出软键盘,要手动弹出
        edittext.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    Layout layout = ((EditText) v).getLayout();
                    float x = event.getX() + edittext.getScrollX();
                    int offset = layout.getOffsetForHorizontal(0, x);
                    if(offset >= 0 && offset < 11){
                        edittext.setSelection(offset);
                    }else if(offset >= 11){
                        edittext.setSelection(11);
                    }
                    showSoftInput();
                }
                return true;
            }
        });
         
        edittext.addTextChangedListener(new TextWatcher() {
            private String preText;
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
            }
            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                    int count) {
                 
            }
            @Override
            public void afterTextChanged(Editable s) {
                String mobile = getMobile(s.toString());
                String nowtext = buildEditTextContent(mobile);
                if (nowtext.equals(preText)) {
                    return;
                }
                // 计算当前的光标位置
                int offset = calCursorOffset(preText, nowtext);
                // 一定要在setTest之前设置preText,否则会StackOverflow
                preText = nowtext;
                edittext.setText(nowtext);
                // 文字发生变化,重新设置光标,否则会跑到最前面
                setCursorPosition(offset);
                if (mobile.length() == 11) {
                    hideSoftInput();
                }
            }
        });
 
        edittext.setText("15012341234");
    }
 
    private void hideSoftInput(){
        edittext.requestFocus();
        hideKeyboard();
        button.requestFocus();
    }
     
    private void showSoftInput(){
        edittext.requestFocus();
        showKeyboard(edittext);
    }
     
    private void setCursorPosition(int offset){
        edittext.setSelection(offset);
    }
     
    private String getMobile(String text){
        if(text == null || text.length() <= 0){
            return "";
        }
        String arr[] = text.split("\\s");
        String mobile = arr[0];
        return mobile;
    }
     
    private String buildEditTextContent(String mobile){
        String name = getName(mobile);
        String text = mobile + (name == null ? "" : " " + name);
        return text;
    }
     
    private int calCursorOffset(String pre, String now){
        if(isBlank(pre) && isBlank(now)){
            return 0;
        }else if(!isBlank(pre) && !isBlank(now)){
            for(int i=0; i<pre.length() &&="" i<now.length();="" i++){="" int="" prechar="pre.charAt(i);" nowchar="now.charAt(i);" if(prechar="" !="nowchar){" return="" i;="" }="" now.length()=""> 11 ? 11 : now.length();
    }
 
    // 业务方法,联系人数据
    private Map<string, string=""> data;
 
    private String getName(String mobile) {
        if (data == null) {
            data = contactData();
        }
        return data.get(mobile);
    }
 
    private Map<string, string=""> contactData() {
        Map<string, string=""> data = new HashMap<string, string="">();
        data.put("15012341234", "张三");
        data.put("15112341234", "李四");
        data.put("15212341234", "王五");
        return data;
    }
     
    // util方法
    public void showKeyboard(View focusView) {
        Object showing = focusView.getTag();
        if (showing != null) {
            return;
        }
        try {
            InputMethodManager imm = (InputMethodManager) this
                    .getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.showSoftInput(focusView, 0);
            focusView.setTag(new Object());
        } catch (Exception e) {
            Log.e("SoftInput:Showing had a wrong.", e.toString());
        }
    }
 
    public void hideKeyboard() {
        View focusView = this.getCurrentFocus();
        if (focusView == null || focusView.getTag() == null) {
            return;
        }
        try {
            InputMethodManager imm = ((InputMethodManager) this
                    .getSystemService(Activity.INPUT_METHOD_SERVICE));
            imm.hideSoftInputFromWindow(focusView.getWindowToken(),
                    InputMethodManager.HIDE_NOT_ALWAYS);
            focusView.setTag(null);
        } catch (Exception e) {
            Log.e("SoftInput:Hiding had a wrong.", e.toString());
        }
    }
 
    public boolean isBlank(String str){
        if(str == null || str.length() <= 0){
            return true;
        }
        return false;
    }
}</string,></string,></string,></string,></pre.length()>


activity_main.xml:

1
2
3
4
5
6
7
8
<linearlayout 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:orientation="vertical" android:id="@+id/rootview">
 
    <edittext android:id="@+id/edittext1" android:layout_width="300dp" android:layout_height="wrap_content">
     
     <button android:id="@+id/button1" android:layout_width="0dp" android:layout_height="0dp">
      
 
</button></edittext></linearlayout>