简体   繁体   中英

onKeyListener not working as i want it to… in android

Currently I am using the following code to set onKeyListener for editText in android.

final EditText etSearch = (EditText) findViewById(R.id.editText3search);
        etSearch.setOnKeyListener(new OnKeyListener() {

            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                // TODO Auto-generated method stub
                MyDbHelper mydbhelper2 = new MyDbHelper(MainActivity.this);
                Course[] abc = mydbhelper2.query_searchByCourseName(etSearch.getText().toString());
                CourseAdapter cadptrSearch = new CourseAdapter(MainActivity.this, R.layout.list_course_row,abc);
                ListView lvSearch = (ListView) findViewById(R.id.list);
                lvSearch.setAdapter(cadptrSearch);
                return false;
            }
        });

The problem I am facing is that when I type in the editText for searching, the new list doesn't show up instantly... Instead for the listView to change/update I have to press backspace... This backspace triggers the search in db and the listview changes its contents according to the data entered in edit text. I want search results to appear as soon I press any key. How to do this?

Thanks.

Option 1:

Use getAction() to determine when any key(excluding the back key) is pressed like this:

  etSearch.setOnKeyListener(new OnKeyListener() {

                    @Override
                    public boolean onKey(View v, int keyCode, KeyEvent event) {
                        // TODO Auto-generated method stub
                       if(event.getAction() == KeyEvent.ACTION_DOWN && keyCode != KeyEvent.KEYCODE_BACK){
                        MyDbHelper mydbhelper2 = new MyDbHelper(MainActivity.this);
                        Course[] abc = mydbhelper2.query_searchByCourseName(etSearch.getText().toString());
                        CourseAdapter cadptrSearch = new CourseAdapter(MainActivity.this, R.layout.list_course_row,abc);
                        ListView lvSearch = (ListView) findViewById(R.id.list);
                        lvSearch.setAdapter(cadptrSearch);
                      }
                        return false;
                    }
                });

If that fails, then you're dealing with a soft keyboard issue so try Option 2:

Option 2: Use a text watcher to handle text changed events:

    etSearch.addTextChangedListener(new TextWatcher () {
            public void afterTextChanged(Editable s) {
            if(s.length() > 0){
                MyDbHelper mydbhelper2 = new MyDbHelper(MainActivity.this);
                        Course[] abc = mydbhelper2.query_searchByCourseName(etSearch.getText().toString());
                        CourseAdapter cadptrSearch = new CourseAdapter(MainActivity.this, R.layout.list_course_row,abc);
                        ListView lvSearch = (ListView) findViewById(R.id.list);
                        lvSearch.setAdapter(cadptrSearch);
}

}

        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
        }

    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM