我正在开发一个应用程序,当应用程序开始运行时我需要禁用虚拟按钮,因为应用程序中有按钮。任何人都可以帮我解决这个问题吗?提前谢谢。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_1st_main);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
@SuppressWarnings({ "unchecked", "rawtypes" })
private final List hijackKeys = new ArrayList(Arrays.asList(
KeyEvent.KEYCODE_VOLUME_DOWN, KeyEvent.KEYCODE_VOLUME_UP,
KeyEvent.KEYCODE_BACK, KeyEvent.KEYCODE_HOME));
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
if (hijackKeys.contains(event.getKeyCode())) {
return true;
} else {
return super.dispatchKeyEvent(event);
}
}
答案 0 :(得分:3)
这是最佳解决方案。
public class BaseActivity extends Activity {
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
Log.d("Focus debug", "Focus changed !");
if(!hasFocus) {
Log.d("Focus debug", "Lost focus !");
Intent closeDialog = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
sendBroadcast(closeDialog);
}
}
} // all credit goes here: http://www.juliencavandoli.com/how-to-disable-recent-apps-dialog-on-long-press-home-button/
答案 1 :(得分:2)
使用以下代码覆盖后退按钮
@Override
public void onBackPressed() {
// TODO Auto-generated method stub
//super.onBackPressed(); // nothing to do here
}
现在,对于菜单按钮尝试这样
@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); // comment this line to disable menu button
return true;
}
禁用菜单按钮的最佳方法是从活动类中删除onCreateOptionsMenu方法...
答案 2 :(得分:0)
我真的不知道这是否有效。请试试这个。
将此内容添加到您的主要活动的manifest.xml中:
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.HOME" />
source found from stack overflow。 快乐的编码..
试试这种方式
@Override
public void onAttachedToWindow() {
//only desable home button
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
super.onAttachedToWindow();
}