Android EditText 输入数字和小数,设置输入的范围0.001-1000

来源:互联网 发布:外包美工 编辑:程序博客网 时间:2024/06/07 01:17

要求实现的效果:EditText的输入数据值的范围是0.001-1000

因为EditText输入的是数字和小数,两种类型。

布局类型:

    <EditText        android:id="@+id/et_num"        android:layout_width="fill_parent"        android:layout_height="60dp"        android:gravity="center"        android:numeric="decimal" />
代码的实现:
/** * 输入框输入值的范围  1000-0.001(EditText的属性:android:numeric="decimal") * @param txtInput */public static void setRegion(EditText txtInput){txtInput.addTextChangedListener(new TextWatcher() {  public void afterTextChanged(Editable edt)   {    String temp = edt.toString();    int posDot = temp.indexOf(".");    //小数点之前保留3位数字或者一千    if (posDot <= 0){    //temp    if(temp.equals("1000")){    return;    }else{    if(temp.length()<=3){    return;    }else{    edt.delete(3, 4);    return;    }    }    }    //保留三位小数    if (temp.length() - posDot - 1 > 3)    {    edt.delete(posDot + 4, posDot + 5);    }  }  public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {    }  public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {    }});}



0 0
原创粉丝点击