1

我正在尝试在 android 中处理事件软键盘,但是当我按下回车键时,我永远不会生成我所做的任何事情?请对我的代码进行任何编辑。

public class MainActivity extends Activity implements OnKeyListener  {
EditText editText1;
 EditText editText2;
      public void 
onCreate(Bundle savedInstanceState) {
      super.onCreate
(savedInstanceState);
      setContentView
(R.layout.activity_main);
      editText1 = (EditText) 
findViewById(R.id.editText1);
editText1.setOnKeyListener(this);
      editText2 = (EditText) findViewById(R.id.editText2);
      editText2.setOnKeyListener(this);
      InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
      imm.showSoftInput(editText1, InputMethodManager.SHOW_IMPLICIT);
       }
     public boolean onKey(View view, int keyCode, KeyEvent event) {
      if (event.getAction() == KeyEvent.KEYCODE_ENTER) 
      {
     editText2.setText("hello");      
      }
      return false; // pass on to other listeners.
     }
    }
4

1 回答 1

0

我认为正确的做法应该是:

 public boolean onKey(View view, int keyCode, KeyEvent event) {
  if (keyCode == KeyEvent.KEYCODE_ENTER) 
  {
       editText2.setText("hello");      
  }
  return true; // pass on to other listeners.
 }
于 2013-03-11T14:22:04.713 回答