監(jiān)聽Android鍵盤@,實現(xiàn)聊天界面@彈出

setOnKeyListener不可靠,硬鍵盤可靠,軟鍵盤不可靠

Interface definition for a callback to be invoked when a hardware key event is dispatched to this view.   
The callback will be invoked before the key event is given to the view.   
This is only useful for hardware keyboards;  
 a software input method has no obligation to trigger this listener.

當(dāng)硬件鍵事件被分派到此視圖時,要調(diào)用的回調(diào)的接口定義。
回調(diào)函數(shù)將在鍵事件被賦予視圖之前被調(diào)用。
這只對硬件鍵盤有用;
軟件輸入法沒有義務(wù)觸發(fā)這個監(jiān)聽器。

使用InputConnection,重寫onCreateInputConnection放在,在commitText方法里,監(jiān)聽按鍵

public class TEditText extends AppCompatEditText {
    public TEditText(Context context) {
        super(context);
    }
 
    public TEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
 
    public TEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
 
    /**
     * 當(dāng)輸入法和EditText建立連接的時候會通過這個方法返回一個InputConnection。
     * 我們需要代理這個方法的父類方法生成的InputConnection并返回我們自己的代理類。
     */
    @Override
    public InputConnection onCreateInputConnection(EditorInfo editorInfo) {
         return new InputConnectionWrapper(super.onCreateInputConnection(editorInfo),false) {
                        public boolean commitText(CharSequence text, int newCursorPosition) {
                            if (text.equals("@")) {

                            }
                            return super.commitText(text, newCursorPosition);
                        }
                    };
    }
 
    
}
 

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容