android format edittext to display spaces after every 4 characters

前端 未结 12 1706
傲寒
傲寒 2020-12-10 13:27

Android - I want to get a number input from the user into an EditText - it needs to be separated by spaces - every 4 characters. Example: 123456781234 -> 1234 5678 1234

12条回答
  •  半阙折子戏
    2020-12-10 13:33

    If someone still looking for answer, check format-edit-text library

    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("---- ---- ---- ----");
        }
    }
    

    Reference https://stackoverflow.com/a/59742478/1679946

提交回复
热议问题