这篇文章主要介绍“Android中如何实现键盘操作”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“Android中如何实现键盘操作”文章能帮助大家解决问题。
一般是如下三个Android键盘操作的函数:
onKeyDown, onKeyUp, on, onKeyMultiple
参看如下Android键盘操作代码:
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) {
// TODO Auto-generated method stub
super.onKeyDown(keyCode, event);
setTitle("you pressed key:" + String.valueOf(keyCode));
return true;
}
@Override
public boolean onKeyMultiple(int keyCode, int repeatCount,
KeyEvent event) {// TODO Auto-generated method stub
super.onKeyMultiple(keyCode, repeatCount, event);
TextView tv = (TextView)this.findViewById(R.id.mainview);
tv.setText("you have press key:[" + String.valueOf(keyCode) + "]
for:" + String.valueOf(repeatCount) + "Times!");return true;
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
super.onKeyUp(keyCode, event);
setTitle("you release key:" + String.valueOf(keyCode));
return true;
}
}
关于“Android中如何实现键盘操作”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注亿速云行业资讯频道,小编每天都会为大家更新不同的知识点。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。