How can I get the first 10 digits from EditText, and set this digits in a TextView?

筅森魡賤 提交于 2019-12-14 03:59:18

问题


I have a field filled with an EditText control, it figures: 475759403048575663648495004945757590.
What can I use to choose the first 10 numbers from this EditText:
and then, insert the numbers 4757594030, in the TextView?


回答1:


You can substring the string to 10 characters.

String s = somestring.substring(0,10);

So this will return the first 10 characters, and now you can put the value S in your control!




回答2:


You can put a textwatcher to the edittext, then count as the edit text is being edited, when you get to the number of characters (10 in this case), you can then display those characters.

Example

TextView textView = ....
edittext.addTextChangedListener(new TextWatcher(){

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) { }

    @Override
    public void afterTextChanged(Editable textVal) {
        String inputText = textVal.toString();
        if (inputText.length() == 10) {
             textView.setText(inputText);
        }
    }
});


来源:https://stackoverflow.com/questions/22143416/how-can-i-get-the-first-10-digits-from-edittext-and-set-this-digits-in-a-textvi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!