Android 界面编程#6-键盘操作响应
2010-08-23 01:39:00 来源:WEB开发网在Android中是通过触屏及键盘来操作程序的,我们如何响应一般的键盘及触笔动作呢?通过对Android一些基本界面元素的操作的了解,如果你再熟悉MVC你可以猜到Android将会如何处理键盘事件,恭喜你,猜对了,仍在Activity中改事件响应函数来做到。一般是如下三个函数:
onKeyDown, onKeyUp, on, onKeyMultiple
参看如下代码:
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
public class TestProgress extends Activity {
private ProgressDialog progress = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(0, Menu.FIRST+1, 1, "Open Progress");
menu.add(0, Menu.FIRST+2, 2, "Exit");
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
switch (item.getItemId())
{
case Menu.FIRST +1:
{
progress = new ProgressDialog(this);
progress.setTitle("Progress!!");
progress.setMessage("Please wait for the operation...");
progress.setCancelable(true);
progress.show();
//progress = ProgressDialog.show(this, "Progress!", "Please wait for operation...");
break;
}
case Menu.FIRST +2:
{
finish();
break;
}
}
return true;
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
赞助商链接