我使用片段,我想检测后退按钮以停止退出应用程序并做其他事情。但是....我尝试过的所有代码都不起作用:我已经在调试模式下进行了测试,断点进入功能,但是当我按下后退按钮并且应用程序退出时,似乎没有一个被调用。
public class MyListFragment extends Fragment implements OnTouchListener, OnFocusChangeListener, OnClickListener, OnKeyListener{
// Test 1
public boolean onKeyDown(int keyCode, KeyEvent event) {
String a=null;
switch(keyCode)
{
case KeyEvent.KEYCODE_CAMERA:
Toast.makeText(getActivity(),"Pressed Camera Button", Toast.LENGTH_SHORT).show();
return true;
case KeyEvent.KEYCODE_1:
Toast.makeText(getActivity(), "Pressed 1", Toast.LENGTH_SHORT).show();
return true;
case KeyEvent.KEYCODE_HOME:
Toast.makeText(getActivity(), "Pressed Home Button", Toast.LENGTH_SHORT).show();
return true;
case KeyEvent.KEYCODE_BACK:
Toast.makeText(getActivity(), "Pressed Back Button", Toast.LENGTH_SHORT).show();
return true;
}
if (keyCode == KeyEvent.KEYCODE_BACK)
{
a=a;
}
return false;
}
// Test 2
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
String b=null;
b=b;
return false;
}
// Test 3
public void onBackPressed() {
String b=null;
b=b;
}
// Test 4
public boolean onKeyUp(int keyCode, KeyEvent event) {
String b=null;
b=b;
return false;
}
}
我不知道为什么没有任何工作,任何想法?