Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
397 views
in Technique[技术] by (71.8m points)

android - How to know key presses in EditText

I need to detect every key press on either a soft or hard keyboard in EditText. I just need to send the characters one at a time as they are pressed and don't need the final text string.

I have tried using an EditText with onKeyPress, but I ran into the problems here with not getting key presses with soft keyboards and TextWatcher isn't a good option because I need each key press. Is there any solution to know all the keypresses (including back, shift, enter... also)?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you have an EditText, then you can use the TextWatcher interface. In my code, search_edit is an EditText.

search_edit.addTextChangedListener(new TextWatcher() {          
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {                                   
        //here is your code
        myadapter.getFilter().filter(s);
        listview.setAdapter(myadapter); 
    }                       
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        // TODO Auto-generated method stub                          
    }                       
    @Override
    public void afterTextChanged(Editable s) {
        // TODO Auto-generated method stub                  
    }
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...