06_键盘事件
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.TextView;
public class MainActivity extends Activity implements OnKeyListener {
StringBuilder builder = new StringBuilder();
TextView textView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
textView = new TextView(this);
textView.setText("Press keys (if you have some)!");
textView.setOnKeyListener(this);
textView.setFocusableInTouchMode(true);
textView.requestFocus();
setContentView(textView);
}
@Override
public boolean onKey(View view, int keyCode, KeyEvent event) {
builder.setLength(0);
switch (event.getAction()) {
case KeyEvent.ACTION_DOWN:
builder.append("down, ");
break;
case KeyEvent.ACTION_UP:
builder.append("up, ");
break;
}
builder.append(event.getKeyCode());
builder.append(", ");
builder.append((char) event.getUnicodeChar());
String text = builder.toString();
Log.d("KeyTest", text);
textView.setText(text);
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK)
return false;
else
return true;
}
}