更新,想要限制输入类型只需要在xml文件里设置android:inputType=“number”(“decimal”)用以达到限制的目的

private class JumpNextClass implements TextWatcher {    private EditText thisView;    private View nextView;    public String str_before;    public JumpNextClass(EditText vThis, View vNext) {        super();        thisView = vThis;        nextView = vNext;    }    @Override //保存输入新字符前的text,如输入错误可更改    public void beforeTextChanged(CharSequence s, int start, int count, int after) {        str_before = s.toString();    }    //判断是否是数字,如果不是则改为原来的text    public void onTextChanged(CharSequence s, int start, int before, int count) {        String str = s.toString();        if (str.contains("\r") || str.contains("\n") || str.contains(" "))        {            thisView.setText(str_before);            thisView.setSelection(start);        }        try {            if ( str.length() == 0 || str == null ) return;            if(str.charAt(str.length()-1) == 'f' || str.charAt(str.length()-1) == 'd'                    || str.charAt(str.length()-1) == 'D' || str.charAt(str.length()-1) == 'F')            {                thisView.setText(str_before);                thisView.setSelection(start);                str = str.substring(0,str.length()-1);            }            double temp = Double.valueOf(str);        } catch ( Exception e ) {            thisView.setText(str_before);            thisView.setSelection(start); //使光标位置不变,不然setText()会让光标移到text最前            Toast.makeText(MainActivity.this, "You should input num.", Toast.LENGTH_SHORT).show();        }    }    public void afterTextChanged(Editable s) {        if (s.toString().contains("\r") || s.toString().contains("\n")) //如果输入了换行符那么跳转        {            if (nextView != null && nextView instanceof EditText) {                nextView.requestFocus();                EditText temp = (EditText)nextView;                temp.setSelection(temp.getText().length());            } else if ( nextView instanceof Button )            {                Button temp = (Button)nextView;                temp.setFocusable(true);                temp.setFocusableInTouchMode(true);                temp.requestFocus();            }        }    }}

更多相关文章

  1. Android通过AES128加密解密字符串
  2. 2011.07.11(3)——— android xliff字符串操作
  3. android EditText 如何限制只能输入指定的字符
  4. android 如何把新建项目的那个 android app project
  5. Android近百个项目的源代码
  6. Android EditText输入光标居于开头最开始位置
  7. android studio 报编码 GBK 的不可映射字符问题

随机推荐

  1. Android(安卓)自定义View探索——图片
  2. 在Android(安卓)Studio中为jar添加source
  3. 2、从头学Android之第一个Activity程序
  4. android之日志打印管理封装类
  5. Android简单实现录音功能
  6. Android电话监听与短信监听
  7. Android内存问题的几种解决方案(二)
  8. 自定义View详解
  9. Android(安卓)进阶之旅 | Android(安卓)
  10. Android自定义控件(一)