简体   繁体   中英

how I can to handle key event in softkeyboard with edittext in android

I am trying to handle event softkeyboard in android but when I press on enter key never generate any thing what I do ?? please any one edit on my code.

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.
     }
    }

I think the right thing should be:

 public boolean onKey(View view, int keyCode, KeyEvent event) {
  if (keyCode == KeyEvent.KEYCODE_ENTER) 
  {
       editText2.setText("hello");      
  }
  return true; // pass on to other listeners.
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM