Clear EditText text after adding onTextChanged implemenation

后端 未结 5 676
孤街浪徒
孤街浪徒 2021-01-11 11:18

I\'m adding a listener to an EditText field to appropriately format the number to currency in real time.

        loanText.addTextChangedListener(new TextWatc         


        
5条回答
  •  太阳男子
    2021-01-11 12:09

    public class MainActivity extends Activity {
    EditText et;
    TextWatcher tw;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        et=(EditText) findViewById(R.id.et);
    
         tw=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 s) {
                et.removeTextChangedListener(tw);
                et.setText(Math.random()+"");//add your logic here 
                et.addTextChangedListener(tw);
            }};
            et.addTextChangedListener(tw);
    }   
    

    } This is a Simple solution.

提交回复
热议问题