在android中格式化信用卡输入,如(xxxx xxxx xxxx xxxx)

时间:2013-12-27 13:07:53

标签: android

如何在android中设置上述格式的信用卡号(中间的空格)?任何人都可以帮我这个吗?

3 个答案:

答案 0 :(得分:2)

如果我没有错,你想这样做,因为用户在每四个字符输入后在edittext中提供输入,后面应该放置一个空格。

如果这是您需要的,那么您可以实施:

editText.addTextChangedListener(new TextWatcher(){
        public void afterTextChanged(Editable s) {
           // apply your logic for putting space after every four characters typed
        }
        public void beforeTextChanged(CharSequence s, int start, int count, int after){}
        public void onTextChanged(CharSequence s, int start, int before, int count){}
    });

修改

我认为你可以使用PatternMatcher课程。请关注[developer.android.com]上的这些链接:

http://developer.android.com/reference/android/os/PatternMatcher.html

http://developer.android.com/reference/java/util/regex/Pattern.html

http://developer.android.com/reference/java/util/regex/Matcher.html

在此处查看教程:http://www.vogella.com/articles/JavaRegularExpressions/article.html

答案 1 :(得分:1)

你可以这样做:

private String _ccNumber = "";

cc_no.addTextChangedListener(ccWatcher);

private TextWatcher ccWatcher= new TextWatcher() {
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {}
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
    @Override
    public void afterTextChanged(Editable s) {
        if(_ccNumber.length() < s.length()){

            switch(s.length()){
                case 6:
                    s.insert(5, " ");
                    break;
                case 11:
                    s.insert(10, " ");
                    break;
                case 16:
                    s.insert(15, " ");
                    break;
            }
        }
        _ccNumber = s.toString();
    }
};

答案 2 :(得分:1)

  //get the reference of this edit text field
 EditText  etNICNO_Sender=(EditText)findViewById(R.id.etNICNO_Sender);
    /*add textChangeListner with TextWatcher argument
        by adding text change listner with text watcher we can get three methods of
        Edit Text 1) onTextChanged 2) beforeTextChanged 3) afterTextChanged
        these methods work when user types in text feild.
     */
 etNICNO_Sender.addTextChangedListener(new TextWatcher() {
   int len=0;
   @Override
   public void onTextChanged(CharSequence s, int start, int before, int count) {
    // TODO Auto-generated method stub

  String str = etNICNO_Sender.getText().toString();

  if((str.length()==5 && len <str.length()) || (str.length()==10 && len <str.length()) || (str.length()==15 && len <str.length())){
                  //checking length  for backspace.
                  etNICNO_Sender.append(" "); //append space
                 }
   }
   @Override
   public void beforeTextChanged(CharSequence s, int start, int count,
     int after) {
    String str = etNICNO_Sender.getText().toString();
                         len = str.length();
        }
   @Override
   public void afterTextChanged(Editable s) {
    // TODO Auto-generated method stub

   }
  });
这对我有用,就像魅力一样!请参阅here了解完整的代码段,您需要自动附加空格,我会自动附加连字符