[英]Setup a button to act as back button
我想設置一個簡單的Alert View的OK按鈕來充當后退按鈕。 我應該使用什么方法?
AlertDialog.Builder alt_bld = new AlertDialog.Builder(this.getContext());
AlertDialog alert = alt_bld.create();
alert.setTitle("Your score is: " + score);
alert.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//here would be the "back button's method"
}});
alert.show();
只需添加:
NameOfYourContainingClass.this.onBackPressed();
您必須調用finish方法才能破壞活動。
AlertDialog.Builder alt_bld = new AlertDialog.Builder(this.getContext());
AlertDialog alert = alt_bld.create();
alert.setTitle("Your score is: " + score);
alert.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Enter you activity Name add call finish.
MyAcitivity.this.finish(); // Or MyAcitivity.this.onBackPressed()
}});
alert.show();
為此,在您的活動和點擊按鈕時調用此按鈕。
@Override
public void onBackPressed()
{
super.onBackPressed();
};
AlertDialog.Builder alt_bld = new AlertDialog.Builder(this.getContext());
AlertDialog alert = alt_bld.create();
alert.setTitle("Your score is: " + score);
alert.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
onBackPressed();
}});
alert.show();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.