我正在尝试添加此功能:
- 单击“返回”按钮时,会弹出一个退出提示(AlertDialog with yes or no)
问题
但它不能正常工作。如果我点击按钮,它会直接退出程序。
以下是我的整个活动代码:(我测试过的退出函数位于代码末尾)
@SuppressWarnings("deprecation")
public class MainScreen extends TabActivity {
// TabSpec Names
private static final String Alerts_SPEC = "Alertes";
private static final String Status_SPEC = "Status";
private static final String Details_SPEC = "Events";
@SuppressWarnings("deprecation")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_screen);
TabHost tabHost = getTabHost();
// Inbox Tab
TabSpec alertsSpec = tabHost.newTabSpec(Alerts_SPEC);
alertsSpec.setIndicator(Alerts_SPEC);
Intent alertsIntent = new Intent(this, FragmentAlerts.class);
alertsSpec.setContent(alertsIntent);
// Outbox Tab
TabSpec statusSpec = tabHost.newTabSpec(Status_SPEC);
statusSpec.setIndicator(Status_SPEC);
Intent statusIntent = new Intent(this, FragmentStatus.class);
statusSpec.setContent(statusIntent);
// Profile Tab
TabSpec detailsSpec = tabHost.newTabSpec(Details_SPEC);
detailsSpec.setIndicator(Details_SPEC);
Intent detailsIntent = new Intent(this, FragmentEvents.class);
detailsSpec.setContent(detailsIntent);
// Adding all TabSpec to TabHost
tabHost.addTab(alertsSpec); // Adding Inbox tab
tabHost.addTab(statusSpec); // Adding Outbox tab
tabHost.addTab(detailsSpec); // Adding Profile tab
}
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_main_actions, menu);
return super.onCreateOptionsMenu(menu);
}
/**
* On selecting action bar icons
* */
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Take appropriate action for each action item click
switch (item.getItemId()) {
case R.id.action_refresh:
// refresh
finish();
startActivity(getIntent());
return true;
case R.id.action_password:
// help action
Intent i = new Intent(getApplicationContext(), LoginActivityAdmin.class);
startActivity(i);
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
// -------------- EXIT CODE BEGINS ------------------------------
@Override
public boolean onKeyDown(int keyCode, KeyEvent event){
if ((keyCode == KeyEvent.KEYCODE_BACK)) { //stop your music here
// To exit application
onBackPressed2();
}
return super.onKeyDown(keyCode, event);
}
public void onBackPressed2() {
new AlertDialog.Builder(this)
.setMessage("Are you sure you want to exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
MainScreen.this.finish();
}
})
.setNegativeButton("No", null)
.show();
}
}
SOLTUION :
我将此代码添加到每个孩子:
@Override
public void onBackPressed() {
this.getParent().onBackPressed();
}
然后在我的主要活动中调用它:
@Override
public void onBackPressed() {
new AlertDialog.Builder(this)
.setMessage("Are you sure you want to exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
MainScreen.this.finish();
}
})
.setNegativeButton("No", null)
.show();
}
答案 0 :(得分:1)
答案 1 :(得分:0)
我之前的回答是错误的,试试这个:
/* you don't need this
@Override
public boolean onKeyDown(int keyCode, KeyEvent event){
if ((keyCode == KeyEvent.KEYCODE_BACK)) { //stop your music here
// To exit application
onBackPressed();
}
return super.onKeyDown(keyCode, event);
} */
@Override
public void onBackPressed() {
new AlertDialog.Builder(this)
.setMessage("Are you sure you want to exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
onBackPressed2();
}
})
.setNegativeButton("No", null)
.show();
}
public void onBackPressed2()
{
MainScreen.this.finish();
}
}
或者执行以下操作:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event){
if ((keyCode == KeyEvent.KEYCODE_BACK)) { //stop your music here
// To exit application
onBackPressed2();
return true;
}
else
{
return super.onKeyDown(keyCode, event);
}
}
答案 2 :(得分:0)
处理关键事件时请勿致电super.onKeyDown(keyCode, event);
。
@Override
public boolean onKeyDown(int keyCode, KeyEvent event){
if ((keyCode == KeyEvent.KEYCODE_BACK)) { //stop your music here
// To exit application
onBackPressed2();
return true;
}
return super.onKeyDown(keyCode, event);
}
答案 3 :(得分:0)
你需要在FragmentAlerts.class中放入onBackPressed()方法。 MainActivity由TabActivity扩展,因此默认调用将转到第一个选项卡。所以打电话给你的onbacpress indside第一个活动,即FragmentAlerts。