Format credit card in edit text in android

后端 未结 29 2021
耶瑟儿~
耶瑟儿~ 2020-11-30 19:18

How to make EditText accept input in format:

4digit 4digit 4digit 4digit 

I tried Custom format edit text input android to acc

29条回答
  •  北海茫月
    2020-11-30 19:47

    If anyone still looking for answer,

    Try the format-edit-text library for auto-formatting text in one line of code. This library uses dash(es) to define the format of the input.

    editText.setFormat("any (dash) format");
    

    How to use

    add format-edit-text library dependency in app/build.gradle

    implementation 'com.androidwidgets:formatedittext:0.2.0'
    

    Add FormatEditText view in activity_main.xml

    
    
    
        
    
    
    

    Set credit-card format to FormatEditText view in MainActivity.java

    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            final FormatEditText editText1 = findViewById(R.id.edit_text_1);
            editText1.setFormat("---- ---- ---- ----");
        }
    }
    

    This will produce the below output

    PS: Make sure parameter inputType is added to the FormatEditText view in the layout file.

    android:inputType="number"
    

提交回复
热议问题