直播app开发搭建,EditText保留小数位数的问题
package com.shentaiwang.jsz.savepatient.util;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
/**
* Created by jsz on 2017/7/22.
* 保留小数位数
*/
public class RetainDecimal {
/**
*
* @param editText EditText
* @param pointNo 保留的小数位数
*/
public static void setRetainPoint(final EditText editText, final int pointNo) {
editText.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
if (s.toString().contains(".")) {
if (s.length() - 1 - s.toString().indexOf(".") > pointNo) {
s = s.toString().subSequence(0,
s.toString().indexOf(".") + pointNo + 1);
editText.setText(s);
editText.setSelection(s.length());
}
}
if (s.toString().trim().substring(0).equals(".")) {
s = "0" + s;
editText.setText(s);
editText.setSelection(2);
}
if (s.toString().startsWith("0")
&& s.toString().trim().length() > 1) {
if (!s.toString().substring(1, 2).equals(".")) {
editText.setText(s.subSequence(0, 1));
editText.setSelection(1);
return;
}
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
}
}
以上就是直播app开发搭建,EditText保留小数位数的问题,更多内容欢迎关注之后的文章
标签:int,EditText,app,toString,editText,小数位 From: https://www.cnblogs.com/yunbaomengnan/p/17349341.html