如何从小部件启动对话框?

4

我找不到任何一个小部件在被点击后能够弹出对话框的案例。你有什么想法吗?

这是我的代码:

public void onUpdate(Context c, AppWidgetManager appWidgetManager,int[] appWidgetIds) {
    final int N = appWidgetIds.length;
    AppWidgetManager mgr = AppWidgetManager.getInstance(c);
     for (int i=0; i<N; i++) {
            int appWidgetId = appWidgetIds[i];
    RemoteViews views = new RemoteViews(c.getPackageName(), R.layout.widget_layout);
    Intent fireDialog = new Intent(c,Execute.class);

    Toast test=Toast.makeText(c.getApplicationContext(),"onUpdate",Toast.LENGTH_LONG);
    test.show();
    PendingIntent pendingIntent = PendingIntent.getActivity(c, appWidgetId, fireDialog, PendingIntent.FLAG_UPDATE_CURRENT);
    views.setOnClickPendingIntent(R.id.Button, pendingIntent);
    Log.w(LOG_TAG,"Called");
    mgr.updateAppWidget(appWidgetId, views);
     }
}

你能详细说明一下吗?你的需求是像这样的,点击一个按钮然后想要出现一个对话框吗? - Sreeram
你能简要描述一下你正在使用哪个小部件吗?我认为你可以尝试在该小部件的onClickListner中使用它。 - School Boy
要求点击应用程序小部件,然后弹出对话框。 - JustCurious
3个回答

10
我假定您在这里所指的“组件”是“应用程序小部件”,即应用程序可以添加到用户主屏幕上的交互元素。
应用程序小部件无法显示对话框,因为只有通过Activity才能完成。不过,您的应用程序小部件可以通过startActivity()启动一个Activity。并且,您可以通过向元素添加android:theme="@android:style/Theme.Dialog"来模仿对话框外观的主题。

是的,我指的是应用程序小部件。我还更新了我的问题,并附上了我目前拥有的代码。 - JustCurious
@JustCurious:你的代码看起来没有什么异常。确保你的Execute活动具有对话主题。 - CommonsWare

2
创建一个主题为对话框的活动,并调用它。
<activity android:name=".DailyPillDialog" android:label="@string/app_name" android:theme="@android:style/Theme.Dialog">

1
当对话框主题启动时,原始应用程序也会在后台启动。有没有办法不启动主应用程序?我的意思是我只想启动对话框框。 - JustCurious

0

您可以创建一个自定义的AlertDialog以满足您的需求。然后,您可以从其他小部件(例如Button)的onClickListener回调中alertDialog.show()来显示对话框。


有可能做到吗?@CommonsWare。我知道如何单独创建对话框,问题是如何将其与我的应用程序小部件相结合。 - JustCurious

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接