EditText常見操作之監聽鍵盤輸入和搜索動作

EditText如果要在鍵盤上顯示搜索按鈕,需要在xml中加入這個屬性:

     android:imeOptions="actionSearch"

在代碼中添加EditText的搜索按鍵響應:

EditText editText = v.findViewById(R.id.edit_text);
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
	@Override
	public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
		switch (i) {
			case EditorInfo.IME_ACTION_SEARCH:
				search();
				break;
			default:
				break;
		}
		return false;
	}
});

監聽鍵盤輸入變化:

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 s) {
                //do something
            }
        });

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章