我的 Activity 的 xml 代码是
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<EditText
android:id="@+id/et1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</RelativeLayout>
为此添加关键事件,我已经在这样的主要活动中编码
public class MainActivity extends Activity
{
EditText e1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
e1=(EditText)findViewById(R.id.et1);
/* e1.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
Log.i("keycode","Inside on key");
//char a=event.getDisplayLabel();
char a = (char)event.getUnicodeChar();
Log.i("keycode",Character.toString(a));
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KEYCODE_ENTER)) {
Log.i("keycode",Integer.toString(keyCode));
Log.i("keycode",(e1.getText()).toString());
return true;
}
else
{
return false;
}
// TODO Auto-generated method stub
}
});*/
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
Log.i("key","KeyPressed"+Integer.toString(keyCode));
Log.i("keycode",(e1.getText()).toString());
return super.onKeyDown(keyCode, event);
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
switch(keyCode)
{
case KeyEvent.KEYCODE_A:
{
Log.i("key1","KeyPressed A");
Log.i("keycode1",(e1.getText()).toString());
return true;
}
}
return super.onKeyUp(keyCode, event);
}
@Override
public void onBackPressed() {
// TODO Auto-generated method stub
new AlertDialog.Builder(this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("Closing Activity")
.setMessage("Are you sure you want to close this activity?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
.setNegativeButton("No", null)
.show();
}
}
当我从软键盘按 A 时...未调用 onKeyDown 或 onKeyUp 方法...有人告诉我当我在软键盘中按字母表时调用这些方法的方法..