我想在android中制作键盘应用程序。
为此,我尝试下面的代码,但它确实无效。
我该怎么做?请帮忙。
public class MainActivity extends Activity {
Button button1;
EditText edit;
KeyEvent keyevent1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button) findViewById(R.id.button);
button1.setOnClickListener(new OnClickListener(){
public void onClick(View v){
keyevent1 = new KeyEvent(KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_ENTER);
System.out.println("aaaa: " + keyevent1);
onKeyDown(KeyEvent.KEYCODE_ENTER, keyevent1);
}
});
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
System.out.println("bbbb: " + event);
return super.onKeyDown(keyCode, event);
}
}
答案 0 :(得分:0)
addCourseText = (EditText) findViewById(R.id.clEtAddCourse);
addCourseText.setOnKeyListener(new OnKeyListener()
{
public boolean onKey(View v, int keyCode, KeyEvent event)
{
if (event.getAction() == KeyEvent.ACTION_DOWN)
{
switch (keyCode)
{
case KeyEvent.KEYCODE_DPAD_CENTER:
case KeyEvent.KEYCODE_ENTER:
addCourseFromTextBox();
return true;
default:
break;
}
}
return false;
}});