AlertDialog不工作 [英] AlertDialog not working

查看:184
本文介绍了AlertDialog不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好我有其中有一个home键,当按钮pressed一个活动,一个警告对话框应该与消息显示不保存退出?
和以下选项(按钮)应提供给用户:

Guys I have an activity which has a home button, when the button is pressed,an alert dialog should appear with the message "Exit without Saving ?" and the following options(buttons) should be available to the user:

1>是

2->否

3>保存并退出

但问题是,当home键是pressed正在显示任何警告对话框。

But the problem is when the home button is pressed NO alert dialog is being displayed.

我尝试以下code:

//这是当按钮pssed $ P $

// this is when the button is pressed

public void onClick(View v) {
    // TODO Auto-generated method stub
switch (v.getId()) {

case R.id.backHome:
        final AlertDialog alertDialog = new AlertDialog.Builder(
                DataView.this).create();
        // alertDialog.setTitle("Exit Without Save ?");
        alertDialog.setMessage("Exit Without Saving");
        alertDialog.setButton("Yes", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                Intent i = new Intent(DataView.this, DiaryActivity.class);
                startActivity(i);
                finish();

            }
        });

        alertDialog.setButton2("No", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                alertDialog.dismiss();

            }
        });
        alertDialog.setButton3("Save and Exit",
                new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        rowIdreceived = getdata.getLong("row_id");
                        String title_updated = topicDisplay.getText()
                                .toString() + " ";
                        String story_updated = StoryField.getText()
                                .toString();
                        DataHolder entry = new DataHolder(DataView.this);

                        try {
                            entry.open();
                        } catch (SQLException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        entry.updateEntry(rowIdreceived, title_updated,
                                story_updated);

                        entry.close();
                        Intent i = new Intent(DataView.this,
                                DiaryActivity.class);
                        startActivity(i);
                        finish();

                    }
                });


        break;

该DiaryActivity是主类。

The DiaryActivity is the main class.

数据视图是当前活动

是按钮简单地退出了本届活动,并回到主要活动即是DiaryActivity。

The Yes button simply exits out of the current activity and goes back the main activity that is the DiaryActivity.

否按钮驳回,那么警报对话框,用户可以保存他的工作并退出后。

The No button "dismisses" the alert dialog and the user can then save his work and exit later.

保存退出按钮的工作保存到数据库,然后退出了当前活动的主要活动。

The save and exit button save the work into the database and then exits out of the current activity to the main activity.

推荐答案

theck出这个。叫 alertDialog.show(); 显示alertdialog

theck out this. call alertDialog.show(); to show alertdialog

AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("Reset...");
alertDialog.setMessage("Are you sure?");
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
   public void onClick(DialogInterface dialog, int which) {
      // here you can add functions
   }
});
alertDialog.setIcon(R.drawable.icon);
alertDialog.show();

这篇关于AlertDialog不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆