Android控件:EditText之setOnEditorActionListener的使用

  • API接口:
    EditText.setOnEditorActionListener
  • API用途:
    給文本編輯框設置監聽事件,不過這個事件,不是點擊事件,也不是編輯事件,而是在編輯完成時點擊軟件盤的回車(當然,是否爲回車按鈕取決於android:imeOptions屬性的設置)按鈕纔會觸發
  • 接口實現
    需要重寫TextView.OnEditorActionListener接口onEditorAction
    onEditorAction參數說明
    1、textView 編輯框輸入的內容,比如點擊了換行也會執行換行後的輸出內容
    2、id 事件標識,其值與android:imeOptions屬性設置的參數有關,與EditorInfo.IME_*做匹配
    3、keyEvent 觸發事件,與KeyEvent.ACTION_*做匹配(備註:keyEvent對象有可能會爲null,原因待研究)
    4、返回值 true表示不關閉軟鍵盤,false表示關閉軟鍵盤
  • 代碼演示:
EditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView textView, int id, KeyEvent keyEvent) {
        if (id == EditorInfo.IME_NULL) {
            return true;
        }
        return false;
    }
});
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章