From 23083dd245d4ffc2ad0fbdd91f0e62e240229494 Mon Sep 17 00:00:00 2001 From: JLo'w Date: Mon, 2 May 2016 11:47:51 +0200 Subject: [PATCH] Filtre regex pour limiter le nombre de décimale --- PremiereActivite/app/src/main/java/com/example/app_10p5/DecimalDigitsInputFilter.java | 29 +++++++++++++++++++++++++++++ PremiereActivite/app/src/main/java/com/example/app_10p5/TabFragment2.java | 9 ++++++++- 2 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 PremiereActivite/app/src/main/java/com/example/app_10p5/DecimalDigitsInputFilter.java diff --git a/PremiereActivite/app/src/main/java/com/example/app_10p5/DecimalDigitsInputFilter.java b/PremiereActivite/app/src/main/java/com/example/app_10p5/DecimalDigitsInputFilter.java new file mode 100644 index 0000000..1803786 --- /dev/null +++ b/PremiereActivite/app/src/main/java/com/example/app_10p5/DecimalDigitsInputFilter.java @@ -0,0 +1,29 @@ +package com.example.app_10p5; + +import android.text.InputFilter; +import android.text.Spanned; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * Created by beaus on 02/05/2016. + */ +public class DecimalDigitsInputFilter implements InputFilter { + + Pattern mPattern; + + public DecimalDigitsInputFilter(int digitsBeforeZero,int digitsAfterZero) { + mPattern= Pattern.compile("[0-9]{0," + (digitsBeforeZero-1) + "}+((\\.[0-9]{0," + (digitsAfterZero-1) + "})?)||(\\.)?"); + } + + @Override + public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) { + + Matcher matcher=mPattern.matcher(dest); + if(!matcher.matches()) + return ""; + return null; + } + +} diff --git a/PremiereActivite/app/src/main/java/com/example/app_10p5/TabFragment2.java b/PremiereActivite/app/src/main/java/com/example/app_10p5/TabFragment2.java index da863b6..deb5800 100644 --- a/PremiereActivite/app/src/main/java/com/example/app_10p5/TabFragment2.java +++ b/PremiereActivite/app/src/main/java/com/example/app_10p5/TabFragment2.java @@ -2,9 +2,11 @@ package com.example.app_10p5; import android.os.Bundle; import android.support.v4.app.Fragment; +import android.text.InputFilter; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import android.widget.EditText; /** * Created by beaus on 24/04/2016. @@ -14,6 +16,11 @@ public class TabFragment2 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - return inflater.inflate(R.layout.tab_fragment_2, container, false); + View v = inflater.inflate(R.layout.tab_fragment_2, container, false);; + + EditText et = (EditText) v.findViewById(R.id.commande_prix); + et.setFilters(new InputFilter[]{new DecimalDigitsInputFilter(3, 2)}); + + return v; } } -- libgit2 0.21.2