如何从另一个活动启动活动的警报对话框

时间:2015-09-02 08:28:09

标签: android widget alertdialog android-alertdialog

在一项活动中,我使用AlertDialog.BuilderLayoutInflater来吸引用户的输入。我在此活动中单击按钮时显示此对话框。这是代码:

buttonPlus.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        LayoutInflater li = LayoutInflater.from(InputDetail.this);
        View promptsView = li.inflate(R.layout.prompt_dialog, null);

        AlertDialog.Builder builder = new AlertDialog.Builder(InputDetail.this);

        builder.setTitle(Html.fromHtml("<font color='"+getResources().getColor(R.color.buttonColor)+"'>"+getResources().getString(R.string.addToAmount)+"</font>"));
        builder.setView(promptsView);
        builder.show();
    }
});

我在小部件上有一个小部件和一个按钮。使用此按钮,我开始InputDetail活动:

Intent intentToInputDetails = new Intent(context, InputDetail.class);
PendingIntent pendingIntentToInputDetails = PendingIntent.getActivity(context, 0, intentToInputDetails, 0);

RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.inputs_widget);
views.setOnClickPendingIntent(R.id.button, pendingIntentToInputDetails);

所以,我的问题是,在我启动InputActivity之后,如何在不等待用户点击按钮的情况下显示AlertDialog

提前致谢。

2 个答案:

答案 0 :(得分:0)

您可以向意图添加数据,然后在新启动的活动中检查该数据。

例如:

Intent intentToInputDetails = new Intent(context, InputDetail.class);
// Also add extra data
intentToInputDetails.putExtra("shouldShowDialog", true);

PendingIntent pendingIntentToInputDetails = PendingIntent.getActivity(context, 0, intentToInputDetails, 0);

RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.inputs_widget);
views.setOnClickPendingIntent(R.id.button, pendingIntentToInputDetails)

在InputDetailActivity中(例如在onCreate()中):

// The second argument is the default value which means that if the argument was not added to the intent, it will be false
boolean shouldShowDialog = getIntent().getBooleanExtra("shouldShowDialog", false);
if (shouldShowDialog)
{
    // Open dialog..
}

答案 1 :(得分:0)

在您的InputDetail活动中,创建一个将对话框显示为

的方法
public void showDialogue(View v) {
    LayoutInflater li = LayoutInflater.from(InputDetail.this);
    View promptsView = li.inflate(R.layout.prompt_dialog, null);

    AlertDialog.Builder builder = new AlertDialog.Builder(InputDetail.this);

    builder.setTitle(Html.fromHtml("<font color='"+getResources().getColor(R.color.buttonColor)+"'>"+getResources().getString(R.string.addToAmount)+"</font>"));
    builder.setView(promptsView);
    builder.show();
}

现在将buttonPlus的OnClickListener中的此方法称为

buttonPlus.setOnClickListener(new View.OnClickListener() {
 @Override
 public void onClick(View v) {
 showDialogue();
 }
});

当调用此活动时,您可以在获取@Hasslam的意图后在OnCreate()中调用此方法...

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add_new_goal);
    boolean shouldShowDialog = getIntent().getBooleanExtra("shouldShowDialog", false);
    if (shouldShowDialog)
    {
       showDialogue();
    }
}

但是当你从小部件开始这个活动时,你必须把这个布尔值放在intent对象中......

Intent intentToInputDetails = new Intent(context, InputDetail.class);
intentToInputDetails.putExtra("shouldShowDialog", true);
PendingIntent pendingIntentToInputDetails = PendingIntent.getActivity(context, 0, intentToInputDetails, 0);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.inputs_widget);
views.setOnClickPendingIntent(R.id.button, pendingIntentToInputDetails)
相关问题