我想执行一个操作,就像我按下按钮一次然后它将移动到所选屏幕,当我按两次后退按钮然后它将显示对话框并要求退出。
我在堆栈溢出中尝试了很多例子,但没有一个能帮助我..
navaigation.java
private int clickCount = 0;
private long delay = 100;
Timer timer = new Timer();
@Override
public void onBackPressed() {
if (clickCount == 2) {
super.onBackPressed();
timer.cancel();
} else{
clickCount++;
timer.schedule(new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
backButtonHandler();
}
});
}
}, delay);
}
}
public void backButtonHandler() {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(
Navigation.this);
// Setting Dialog Title
alertDialog.setTitle("Leave application?");
// Setting Dialog Message
alertDialog.setMessage("Are you sure you want to leave the application?");
// Setting Icon to Dialog
alertDialog.setIcon(R.drawable.m_visit);
// Setting Positive "Yes" Button
alertDialog.setPositiveButton("YES",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
// Setting Negative "NO" Button
alertDialog.setNegativeButton("NO",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Write your code here to invoke NO event
dialog.cancel();
}
});
// Showing Alert Message
alertDialog.show();
}
我试过这种方式,但它无法正常工作......
答案 0 :(得分:0)
嘿,如果用户第一次点击后退按钮,请查看此代码我已经显示Snackbar。第二次应用程序将退出。
private boolean mIsBackAlreadyPressed;
@Override
public void onBackPressed() {
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else if (mIsBackAlreadyPressed) {
mIsBackAlreadyPressed = false;
super.onBackPressed();
overridePendingTransition(R.anim.pull_in_left, R.anim.push_out_right);
} else {
mIsBackAlreadyPressed = true;
Snackbar.make(drawer, R.string.press_back_twice, Snackbar.LENGTH_SHORT).show();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
mIsBackAlreadyPressed = false;
}
}, 2000);
}
}
希望这有帮助。快乐编码。
答案 1 :(得分:0)
试试这段代码:
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.Toast;
public class MainActivity extends Activity {
private static final int TIME_DELAY = 2000;
private static long back_pressed;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public void onBackPressed() {
if (back_pressed + TIME_DELAY > System.currentTimeMillis()) {
super.onBackPressed();
}
else {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setMessage("Are you sure, You wanted to exit");
alertDialogBuilder.setPositiveButton("yes",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
finish();
}
});
alertDialogBuilder.setNegativeButton("No",new
DialogInterface.OnClickListener() {
Override
public void onClick(DialogInterface dialog, int which) {
dismiss();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
}
back_pressed = System.currentTimeMillis();
}
答案 2 :(得分:0)
你能试试吗
private boolean exit=false;//declare in public
public void onBackPressed() {
// TODO Auto-generated method stub
if (exit) {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
finish();
} else {
Toast.makeText(this, "Tap again to exit.", Toast.LENGTH_SHORT)
.show();
exit = true;
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
exit = false;
}
}, 3 * 1000);
}
}
答案 3 :(得分:0)
试试这个:
private Boolean close_app = false;
@Override
public void onBackPressed() {
if (close_app) {
//finish(); // pressed twice
backButtonHandler();
} else {
Toast.makeText(this, "Press Back again to Exit.",
Toast.LENGTH_SHORT).show();
close_app = true;
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
close_app = false;
}
}, 3 * 1000);
}
}
这里Handler
处理后退,只显示Toast
,如果在3秒内再次按下,则会显示退出应用程序的对话框
答案 4 :(得分:0)
如果按回去,那么你可以去任何你想要的地方
@Override
public void onBackPressed() {
super.onBackPressed();
Intent intent = new Intent(Exit.this,Home.class);
startActivity(intent);
finish();
}
如果你现在双击后退按钮,你可以退出尝试下面的代码
private Boolean exit = false;
@Override
public void onBackPressed() {
if (exit) {
finish(); // finish activity
} else {
Toast.makeText(this, "Press Back again to Exit.",
Toast.LENGTH_SHORT).show();
exit = true;
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
exit = false;
}
}, 3 * 1000);
}
}