【问题标题】:How to start an activity if a button pressed on dialog box如果在对话框上按下按钮,如何启动活动
【发布时间】:2016-05-23 09:30:43
【问题描述】:

我使用下面的代码来显示一个对话框,如果单击肯定按钮,我会尝试转到主页。但似乎 IDE 无法解析 startActivity 方法。在onClick 事件中启动活动的正确方法是什么?

 new AlertDialog.Builder(context).setTitle("Blocked!")
                .setMessage("This application is restricted by administrator")
                .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {

                        Intent startHomescreen=new Intent(Intent.ACTION_MAIN);
                        startHomescreen.addCategory(Intent.CATEGORY_HOME);
                        startHomescreen.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                        startActivity(startHomescreen);
                    }
                })       .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        // do nothing
                    }
                })
                .setIcon(android.R.drawable.ic_dialog_alert)
                .show();

【问题讨论】:

  • 使用 context.startActivity()

标签: android android-activity dialog


【解决方案1】:

context.startActivity(startHomescreen);

参考其他一些帖子

  1. Android: How to start an Activity from an alert dialog

【讨论】:

    【解决方案2】:
    context.startActivity(startHomescreen);
    

    我的朋友,它会起作用的!

    【讨论】:

      猜你喜欢
      • 2015-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-10
      • 1970-01-01
      相关资源
      最近更新 更多