0
  • 最佳答案

    EditText限制输入2位小数功能完善

    https://www.sunofbeach.net/a/1351472282035376128


    经过我自己测试,除了部分输入法可以改变光标导致失效外,没发现其他问题。

    1204736502274318336  评论     打赏       A lonely cat
    • 断点-含光君  回复 @A lonely cat
      正则还是第一次的:String regex = "[0-9]*+(\\.[0-9]{0,2})?"; 过滤代码增加: if ("0".equals(dest.toString()) && "0".contentEquals(source)) { return "."; }
      断点-含光君 2021-01-20 09:49   回复 1139423796017500160
    • 断点-含光君  回复 @A lonely cat
      当然,你那个正则和过滤配合还行。 对于屏蔽指针复制这个确实不错。
      断点-含光君 2021-01-20 09:50   回复 1139423796017500160
    • A lonely cat  回复 @断点-含光君
      不屏蔽掉,那你的正则就废了~
      A lonely cat 2021-01-20 09:51   回复 1204736502274318336
    • A lonely cat  回复 @断点-含光君
      之前的equals也行的 我只是看到AS报警告,顺手替换了,哈哈
      A lonely cat 2021-01-20 09:53   回复 1204736502274318336
  • https://blog.csdn.net/heming9174/article/details/68941929

    太久没写安卓了 这个应该可以

    1212331796029628416  评论     打赏       Alex777
    • Alex777  回复 @Alex777
      https://blog.csdn.net/qq_35988274/article/details/100218841
      Alex777 2021-01-19 12:05   回复 1212331796029628416
    • 断点-含光君  回复 @Alex777
      00,00000开头还是无法处理的
      断点-含光君 2021-01-19 14:16   回复 1139423796017500160
  • public static void main(String args[]) {
    		String[] strs = {
    			"0.1",
    			"0.02",
    			"0.003",
    			"00001.2",
    			"01.00",
    			"2341.00",
    			"2341.00098",
    			"10.98",
    			"0100.870",
    		};
    		String pattern = "([1-9]\\d*|0)(\\.\\d{1,2})?";	
    		Pattern r = Pattern.compile(pattern);
    		for(int i = 0;i<strs.length;i++){
    			String str = strs[i];
    			Matcher m = r.matcher(str);
    			System.out.println(str + " == > " + m.matches());	
    		}
    	}
    


    这是我的验证结果:



    1153952789488054272  评论     打赏       拉大锯
    • 拉大锯  回复 @拉大锯
      改成这个([1-9]\\d*|0)(\\.[0-9]{0,2})?
      拉大锯 2021-01-19 14:32   回复 1153952789488054272
相关问题
2020-12-06 23:17 713 18
幻影~ · 提问
2024-04-13 20:13 7 2
幻影~ · 找工作
2024-04-07 10:44 11 2
幻影~ · 问题
2024-03-31 17:20 7 2
TONYGFX · AOSP
2024-03-28 17:11 4 2