我正在嘗試在Canvas
中寫入文字。由於我需要顯示軟鍵盤來編寫文本,因此我在0寬度的活動中添加了一個EditText
。我還實現了一個TextWatcher以獲取輸入到EditText
中的文本。有了這一招,我可以顯示軟鍵盤,每當我用這個代碼,如:當軟鍵盤以編程方式顯示時如何捕捉按鍵事件?
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(myEditText, InputMethodManager.SHOW_IMPLICIT);
像這樣我能夠知道用戶正在寫,寫我Canvas
內的文本。
現在......當用戶想要停止寫作(或者說,將文本固定在畫布上)時,這變得非常棘手。我認爲他可以按'回車'。所以我試圖通過某種方式來抓住關鍵事件。目前爲止沒有任何成功。
這是我的實際代碼。當我想開始寫作時調用此方法。 '編輯'是EditText
。
public void handleUp(final Paint myPaint) {
edit.setFocusable(true);
edit.setFocusableInTouchMode(true);
edit.requestFocus();
edit.addTextChangedListener(new Watcher());
edit.setImeOptions(EditorInfo.IME_ACTION_GO);
edit.setOnEditorActionListener(new OnEditorActionListener() {
public boolean onEditorAction(TextView arg0, int arg1, KeyEvent arg2) {
Log.d("MyApp", "key pressed");
Paint localPaint = new Paint();
mCanvas.drawText(edit.getText().toString(), mX, mY, localPaint);
return false;
}
});
edit.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
Log.d("MyApp", "key pressed");
if (keyCode == KeyEvent.ACTION_DOWN) {
Paint localPaint = new Paint();
mCanvas.drawText(edit.getText().toString(), mX, mY, localPaint);
return true;
}
return false;
}
});
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(edit, InputMethodManager.SHOW_IMPLICIT);
}
當我調試我的應用我從來沒有達到檢查,我已經把我的Log()
,在我的日誌看不到任何消息都不點。我在StackOverFlow中看到過很多這類實現被使用的帖子,我無法弄清楚爲什麼它會在這裏失敗。
謝謝