在J文本字段中遮罩一些字符

当用户在用户UI上键入时,我需要屏蔽数字,用户应该看到一个屏蔽的数字,但在Java代码上,我应该得到整个数字,包括屏蔽的字符,这是用户应该看到的4545********9632,但在Java代码(后面)上,我应该得到整个数字,包括屏蔽的字符。我试过了,但不起作用,它显示整个数字。MaskFormatterJFormattedTextField


try {

     MaskFormatter mask=new MaskFormatter("####********####");

      JFormattedTextField js=new JFormattedTextField();

     mask.install(js);

  } catch (ParseException ex) {

      Logger.getLogger(Masker.class.getName()).log(Level.SEVERE, null, ex);

  }


素胚勾勒不出你
浏览 84回答 2
2回答

ITMISS

这是我的怀疑,class ACustomJEditText extends JTextField{ArrayList<String> realText=new ArrayList<String>();String displayText="";public ACustomJEditText() {&nbsp; &nbsp; // TODO Auto-generated constructor stub&nbsp; &nbsp; super();&nbsp; &nbsp; addKeyListener(new KeyAdapter() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void keyReleased(KeyEvent e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // DELETE TEXT on backspace&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(e.getKeyCode()==KeyEvent.VK_BACK_SPACE) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(realText!=null && realText.isEmpty()==false) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; realText.remove(realText.size()-1);//remove character&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; turnRealTextToString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //set the display text here&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setText(displayText);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //avoid any input if string actually string size is greater than 16&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(realText.size()==16) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setText(displayText);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //other keys should now be added to the input for only numbers&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int input=Integer.parseInt(e.getKeyChar()+"");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //add int to realtext&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; realText.add(input+"");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //turn real text to ####********#### string&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; turnRealTextToString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setText(displayText);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }catch (Exception ex) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Other keys fail.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setText(displayText);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; private void turnRealTextToString() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String result="";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(int i=0;i<realText.size();i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(i>3 && i<12) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result+="*";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result+=realText.get(i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String realDisplay=realText.toString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("DISPLAY: "+result+" REAL: "+getRealText());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //set result to display text&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; displayText=result;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setText(displayText);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });}//get the actual real textpublic String getRealText() {&nbsp; &nbsp; StringBuilder real=new StringBuilder();&nbsp; &nbsp; realText.forEach(text->{&nbsp; &nbsp; &nbsp; &nbsp; real.append(text);&nbsp; &nbsp; });&nbsp; &nbsp; return real.toString();}}应该像魔术一样工作。

PIPIONE

您可以使用正则表达式:var cardnumber = '4567 6365 7987 3783';var first4 = cardnumber.substring(0, 4);var last5 = cardnumber.substring(cardnumber.length - 5);mask = cardnumber.substring(4, cardnumber.length - 5).replace(/\d/g,"*");console.log(first4 + mask + last5);或者,如果您可以尝试以下操作:<html><head><script&nbsp; src="https://code.jquery.com/jquery-3.3.1.min.js"&nbsp; integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="&nbsp; crossorigin="anonymous"></script></head><body><input class="form-control" id="myinput" data-length="12" name="name"></body></html><script>let initial = 4;let maskChars = 6;let realNumber = "";$(function() {&nbsp; $('#myinput').keyup(function(e) {realNumber += this.value[this.value.length-1];&nbsp; &nbsp; if (this.value.length >= initial && this.value.length <= initial + maskChars) {&nbsp; &nbsp; &nbsp; this.value = this.value.slice(0, -1) + '*';&nbsp; &nbsp; }&nbsp; });});</script>这就是我在JS上的工作方式。想法是一样的。如果长度大于所需的数字长度,则可以将字符替换为 *。我还没有尝试过这个Java代码。欲了解更多信息,请访问此链接: https://www.javacodeexamples.com/mask-part-of-string-example-java/878package com.javacodeexamples.stringexamples;import org.apache.commons.lang3.StringUtils;public class MaskStringExample {&nbsp; &nbsp; public static void main(String[] args) throws Exception {&nbsp; &nbsp; &nbsp; &nbsp; String str = "1234567812345678";&nbsp; &nbsp; &nbsp; &nbsp; //mask first 4 characters&nbsp; &nbsp; &nbsp; &nbsp; System.out.println( maskString(str, 0, 4, '*') );&nbsp; &nbsp; &nbsp; &nbsp; //mask everything but last 4 digits&nbsp; &nbsp; &nbsp; &nbsp; System.out.println( maskString(str, 0, 12, '*') );&nbsp; &nbsp; &nbsp; &nbsp; //mask everything&nbsp; &nbsp; &nbsp; &nbsp; System.out.println( maskString(str, 0, str.length(), '*') );&nbsp; &nbsp; &nbsp; &nbsp; //mask everything but first and last 4 digits&nbsp; &nbsp; &nbsp; &nbsp; System.out.println( maskString(str, 1, 12, '*') );&nbsp; &nbsp; }&nbsp; &nbsp; private static String maskString(String strText, int start, int end, char maskChar)&nbsp; &nbsp; &nbsp; &nbsp; throws Exception{&nbsp; &nbsp; &nbsp; &nbsp; if(strText == null || strText.equals(""))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return "";&nbsp; &nbsp; &nbsp; &nbsp; if(start < 0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; start = 0;&nbsp; &nbsp; &nbsp; &nbsp; if( end > strText.length() )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end = strText.length();&nbsp; &nbsp; &nbsp; &nbsp; if(start > end)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new Exception("End index cannot be greater than start index");&nbsp; &nbsp; &nbsp; &nbsp; int maskLength = end - start;&nbsp; &nbsp; &nbsp; &nbsp; if(maskLength == 0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return strText;&nbsp; &nbsp; &nbsp; &nbsp; String strMaskString = StringUtils.repeat(maskChar, maskLength);&nbsp; &nbsp; &nbsp; &nbsp; return StringUtils.overlay(strText, strMaskString, start, end);&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java