Commit 23083dd245d4ffc2ad0fbdd91f0e62e240229494
1 parent
aebe700b
Filtre regex pour limiter le nombre de décimale
Showing
2 changed files
with
37 additions
and
1 deletions
Show diff stats
PremiereActivite/app/src/main/java/com/example/app_10p5/DecimalDigitsInputFilter.java
0 → 100644
@@ -0,0 +1,29 @@ | @@ -0,0 +1,29 @@ | ||
1 | +package com.example.app_10p5; | ||
2 | + | ||
3 | +import android.text.InputFilter; | ||
4 | +import android.text.Spanned; | ||
5 | + | ||
6 | +import java.util.regex.Matcher; | ||
7 | +import java.util.regex.Pattern; | ||
8 | + | ||
9 | +/** | ||
10 | + * Created by beaus on 02/05/2016. | ||
11 | + */ | ||
12 | +public class DecimalDigitsInputFilter implements InputFilter { | ||
13 | + | ||
14 | + Pattern mPattern; | ||
15 | + | ||
16 | + public DecimalDigitsInputFilter(int digitsBeforeZero,int digitsAfterZero) { | ||
17 | + mPattern= Pattern.compile("[0-9]{0," + (digitsBeforeZero-1) + "}+((\\.[0-9]{0," + (digitsAfterZero-1) + "})?)||(\\.)?"); | ||
18 | + } | ||
19 | + | ||
20 | + @Override | ||
21 | + public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) { | ||
22 | + | ||
23 | + Matcher matcher=mPattern.matcher(dest); | ||
24 | + if(!matcher.matches()) | ||
25 | + return ""; | ||
26 | + return null; | ||
27 | + } | ||
28 | + | ||
29 | +} |
PremiereActivite/app/src/main/java/com/example/app_10p5/TabFragment2.java
@@ -2,9 +2,11 @@ package com.example.app_10p5; | @@ -2,9 +2,11 @@ package com.example.app_10p5; | ||
2 | 2 | ||
3 | import android.os.Bundle; | 3 | import android.os.Bundle; |
4 | import android.support.v4.app.Fragment; | 4 | import android.support.v4.app.Fragment; |
5 | +import android.text.InputFilter; | ||
5 | import android.view.LayoutInflater; | 6 | import android.view.LayoutInflater; |
6 | import android.view.View; | 7 | import android.view.View; |
7 | import android.view.ViewGroup; | 8 | import android.view.ViewGroup; |
9 | +import android.widget.EditText; | ||
8 | 10 | ||
9 | /** | 11 | /** |
10 | * Created by beaus on 24/04/2016. | 12 | * Created by beaus on 24/04/2016. |
@@ -14,6 +16,11 @@ public class TabFragment2 extends Fragment { | @@ -14,6 +16,11 @@ public class TabFragment2 extends Fragment { | ||
14 | 16 | ||
15 | @Override | 17 | @Override |
16 | public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { | 18 | public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { |
17 | - return inflater.inflate(R.layout.tab_fragment_2, container, false); | 19 | + View v = inflater.inflate(R.layout.tab_fragment_2, container, false);; |
20 | + | ||
21 | + EditText et = (EditText) v.findViewById(R.id.commande_prix); | ||
22 | + et.setFilters(new InputFilter[]{new DecimalDigitsInputFilter(3, 2)}); | ||
23 | + | ||
24 | + return v; | ||
18 | } | 25 | } |
19 | } | 26 | } |