我正在尝试通过以下代码处理 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 (keyCode == KeyEvent.KEYCODE_B|| keyCode ==KeyEvent.KEYCODE_5)
{
editText2.setText("hello");
return true;
}
return false; // pass on to other listeners.
}
}
但是当我按 b 或任何键时永远不会产生任何东西
当我按下数字键时产生了事件
请任何人帮助我并编辑我的代码。
感谢您的帮助,但我整理了该代码并且无法正常工作,并且是新代码:
public class MainActivity extends Activity {
EditText editText1;
EditText editText2;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText1 = (EditText) findViewById(R.id.editText1);
editText2 = (EditText) findViewById(R.id.editText2);
// InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
// imm.showSoftInput(editText1, InputMethodManager.SHOW_IMPLICIT);
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_B|| keyCode ==KeyEvent.KEYCODE_5)
{
editText2.setText("hello");
return true;
}
return false; // pass on to other listeners.
}
}
请关注我的问题我的问候