0

我正在嘗試在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中看到過很多這類實現被使用的帖子,我無法弄清楚爲什麼它會在這裏失敗。

謝謝

回答

-1

好了,最後不了了之的原因,工作是因爲我EditText有0的寬度當我把寬度和高度1.設置能見度View.INVISIBLE在這種情況下不起作用。

順便說一下,三個Listener(OnEditorActionListener,OnKeyListener和Overlading dispatchKeyEvent)獲得回調。但我會使用OnEditorActionListener,因爲它是唯一一個只獲得一個回調的人。其他兩個回調有問題。

0

從文檔摘自:

public void setOnKeyListener (View.OnKeyListener l) 
Added in API level 1 
Register a callback to be invoked when a hardware key is pressed in this view. Key presses in software input methods will generally not trigger the methods of this listener. 

所以,你應該尋找另外的監聽器。

我最好的猜測是使用這樣的:

public void setOnEditorActionListener (TextView.OnEditorActionListener l) 
Added in API level 3 
Set a special listener to be called when an action is performed on the text view. This will be called when the enter key is pressed, or when an action supplied to the IME is selected by the user. Setting this means that the normal hard key event will not insert a newline into the text view, even if it is multi-line; holding down the ALT modifier will, however, allow the user to insert a newline character. 

但沒有的我的文檔中看到的方法提及的軟鍵盤輸入任何東西。

0

您覆蓋dispatchKeyEvent得到回車鍵

@Override 
public boolean dispatchKeyEvent(KeyEvent event) 
{ 
    if (event.getKeyCode() == KeyEvent.KEYCODE_ENTER) 
    { 
     // Do whatever you want 
    } 

    return super.dispatchKeyEvent(event); 
}