2014-09-25 36 views
0

我想要在android中按下屏幕鍵盤上的某個鍵時執行java方法。例如,用戶打開鍵盤並按下「T」(或任何其他鍵),然後調用該功能。如果按下新鍵,則再次調用該功能等。等等。Android - 當在屏幕鍵盤上按下一個鍵時調用方法android

我看了看網站上的其他類似問題,但沒有一個似乎起作用。這是我目前使用的代碼:

EditText chatbox = (EditText) findViewById(R.id.chatbox); 
    chatbox.setOnEditorActionListener(new OnEditorActionListener() { 

     @Override 
     public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { 

       if (event.getAction() == KeyEvent.ACTION_DOWN) 
       { 
        is_typing(); 
        Log.d("Key pressed", "A key was definitely pressed"); 
       } 
     return true; 
     } 
    }); 

回答

1

嘗試用「TextWatcher」。下面是相同

 EditText editText = new EditText(this); 
    editText.addTextChangedListener(new TextWatcher() { 

     @Override 
     public void onTextChanged(CharSequence s, int start, int before, int count) { 
      // do the stuff , you need to do 

     } 

     @Override 
     public void beforeTextChanged(CharSequence s, int start, int count, 
       int after) { 

     } 

     @Override 
     public void afterTextChanged(Editable s) { 

     } 
    }); 
+0

由於示例代碼,即完美。 – allStack 2014-09-26 02:32:12

相關問題