EditText 自动保留两位小数
2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>
package com.example.astart;
import java.text.DecimalFormat;
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.widget.EditText;
public class MyUtils {
?? ?/**
?? ? * 格式化小數(shù)
?? ? * @param context
?? ? * @param paEditTexts
?? ? * @param limit 保留小數(shù)位數(shù)
?? ? */
?? ?public static void setEditTextAccuracys(final Context context,final int limit,EditText...paEditTexts){
?? ??? ?if(paEditTexts == null) return;
?? ??? ?for (int i = 0; i < paEditTexts.length; i++) {
?? ??? ??? ?setEditTextAccuracy(context,paEditTexts[i],limit);
?? ??? ?}
?? ?}
?? ?
?? ?/**
?? ? * 格式化小數(shù)
?? ? * @param context
?? ? * @param editText
?? ? * @param limit 0 不限制小數(shù)位數(shù)
?? ? */
?? ?private static void setEditTextAccuracy(final Context context,final EditText editText, final int limit) {
?? ? ? ?editText.addTextChangedListener(new TextWatcher() {
?? ? ? ? ? ?@Override
?? ? ? ? ? ?public void onTextChanged(CharSequence s, int start, int before, int count) {
?? ? ? ? ? ? ? ?if(limit != 0){
?? ? ? ? ? ? ? ??? ?/**
?? ? ? ? ? ? ? ??? ? * 限制輸入金額最多為 limit 位小數(shù)
?? ? ? ? ? ? ? ??? ? */
?? ? ? ? ? ? ? ??? ?if (s.toString().contains(".")) {
?? ? ? ? ? ? ? ??? ??? ?if (s.length() - 1 - s.toString().indexOf(".") > limit) {
?? ? ? ? ? ? ? ??? ??? ??? ?s = s.toString().subSequence(0,
?? ? ? ? ? ? ? ??? ??? ??? ??? ??? ?s.toString().indexOf(".") + limit + 1);
?? ? ? ? ? ? ? ??? ??? ??? ?editText.setText(s);
?? ? ? ? ? ? ? ??? ??? ??? ?editText.setSelection(s.length());
?? ? ? ? ? ? ? ??? ??? ?}
?? ? ? ? ? ? ? ??? ?}
?? ? ? ? ? ? ? ?}
?? ? ? ? ? ? ? ?/**
?? ? ? ? ? ? ? ? * 第一位輸入小數(shù)點(diǎn)的話自動(dòng)變換為 0.
?? ? ? ? ? ? ? ? */
?? ? ? ? ? ? ? ?if (s.toString().trim().substring(0).equals(".")) {
?? ? ? ? ? ? ? ? ? ?s = "0" + s;
?? ? ? ? ? ? ? ? ? ?editText.setText(s);
?? ? ? ? ? ? ? ? ? ?editText.setSelection(2);
?? ? ? ? ? ? ? ?}
?? ? ? ? ? ? ? ?/**
?? ? ? ? ? ? ? ? * 避免重復(fù)輸入小數(shù)點(diǎn)前的0 ,沒有意義
?? ? ? ? ? ? ? ? */
?? ? ? ? ? ? ? ?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 edit) {
?? ??? ??? ?}
?? ? ? ?});
?? ? ? ?
?? ? ? ?if(limit != 0){
?? ? ? ??? ?editText.setOnFocusChangeListener(new OnFocusChangeListener() {
?? ? ? ??? ??? ?
?? ? ? ??? ??? ?@Override
?? ? ? ??? ??? ?public void onFocusChange(View view, boolean hasFocus) {
?? ? ? ??? ??? ??? ?if(!hasFocus){
?? ? ? ??? ??? ??? ??? ?if(!editText.getText().toString().isEmpty()){
?? ? ? ??? ??? ??? ??? ??? ?try {
?? ? ? ??? ??? ??? ??? ??? ??? ?double value = Double.parseDouble(editText.getText().toString());
?? ? ? ??? ??? ??? ??? ??? ??? ?DecimalFormat format = new DecimalFormat("0.00");
?? ? ? ??? ??? ??? ??? ??? ??? ?editText.setText(format.format(value));
?? ? ? ??? ??? ??? ??? ??? ?} catch (Exception e) {
?? ? ? ??? ??? ??? ??? ??? ??? ?e.printStackTrace();
?? ? ? ??? ??? ??? ??? ??? ?}
?? ? ? ??? ??? ??? ??? ?}
?? ? ? ??? ??? ??? ?}
?? ? ? ??? ??? ?}
?? ? ? ??? ?});
?? ? ? ?}
?? ? ? ?
?? ?}
}
?
?
使用
?
import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;
public class MainActivity extends Activity {
private EditText monkey;
?? ?@Override
?? ?protected void onCreate(Bundle savedInstanceState) {
?? ??? ?super.onCreate(savedInstanceState);
?? ??? ?setContentView(R.layout.activity_main);
?? ??? ?monkey=(EditText) findViewById(R.id.et_monkey);
?? ??? ?MyUtils.setEditTextAccuracys(this, 2, monkey);
????//上下文,最多保留幾位小數(shù),EditText控件名
?? ?}
}
轉(zhuǎn)載于:https://my.oschina.net/u/2531348/blog/726851
總結(jié)
以上是生活随笔為你收集整理的EditText 自动保留两位小数的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。