Android: why is my OnKeyListener() not called?

前端 未结 3 1722
情话喂你
情话喂你 2021-01-01 11:33

I defined an EditText-field and I want to be informed when the user edits that fields. So I thought: simple - I add an OnKeyListener and so I did. But even though the text f

3条回答
  •  执念已碎
    2021-01-01 11:55

    I had the exact same problem, but on only 1 of my Android apps and I never did figure out what the difference was.

    My solution though was to do what Mayra suggested and add a TextWatcher to handle the TextChanged events. So it works no matter how the text entry occurs.

    editName.addTextChangedListener(new TextWatcher () {
            public void afterTextChanged(Editable s) {
                Button btnSave = (Button)findViewById(R.id.btnSaveToon);
                if(s.length() > 0)
                    btnSave.setEnabled(true);
                else
                    btnSave.setEnabled(false);
            }
    
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                // TODO Auto-generated method stub
            }
    
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // TODO Auto-generated method stub
            }
    
        });
    

    Works like a charm in the emulator and on my HTC Inspire

提交回复
热议问题