2013-06-19 30 views
1

我想在ENTER上得到一个自定义动作来工作。这里是我的EditText上Android键盘中的自定义动作按钮

<EditText 
    android:id="@+id/editor" 
    android:singleLine="true" 
    android:inputType="text" 
    android:imeActionId="@+id/submit_action" 
    android:imeActionLabel="Submit" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:paddingTop="15dp" /> 

然而,当我试图抓住这一行动,我看到动作ID是5,而不是无论是在submit_action

mNameEditor.setOnEditorActionListener(new OnEditorActionListener() { 
    @Override 
    public boolean onEditorAction(TextView arg0, int actionId, KeyEvent event) { 
     Toast.makeText(getActivity(), Integer.toString(actionId) ,Toast.LENGTH_LONG).show(); 
     if (event != null && event.getAction() != KeyEvent.ACTION_DOWN) { 
      return false; 
     } else if(actionId == R.id.submit_action){ 
      //do something 
     } 
     return true; 
    }  
}); 

回答

0

您可以使用以下方法XML:

public boolean onKey(View v, int keyCode, KeyEvent event) { 
    // TODO Auto-generated method stub 
    if (event.getAction() == KeyEvent.ACTION_DOWN) { 
     switch (keyCode) { 
     case KeyEvent.KEYCODE_DPAD_CENTER: 
     case KeyEvent.KEYCODE_ENTER: 

      InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
      imm.hideSoftInputFromWindow(v.getApplicationWindowToken(), 0); 

      return true; 

     default: 
      break; 
     } 
    } 
    return false; 
} 

在这个例子中,我试图关闭IME,但是当按下ENTER键时你可以做任何你想做的事。希望它有帮助。