Masked Input Using EditText Widget in Android

后端 未结 6 969
花落未央
花落未央 2020-12-01 12:48

Is there a way I can specify an input mask to the EditText control in Android?

I want be able to specify something like ### - ## - #### for a Social Security Number.

6条回答
  •  余生分开走
    2020-12-01 13:18

    String phoneNumber = "1234567890";
    
    String text = String.valueOf( android.telephony.PhoneNumberUtils.formatNumber(phoneNumber) ); //formatted: 123-456-7890
    
    editTextMobile.setText(text); //set editText text equal to new formatted number
    
    editTextMobile.setSelection(text.length()); //move cursor to end of editText view
    

    Use this in an onKey function for delicious on-the-fly bunny sex with formatted phone numbers.

提交回复
热议问题