如何从另一个 activity 启动 activity 的警告对话框
how to start activity's alert dialog from another activity
在一个 activity 中,我使用 AlertDialog.Builder
和 LayoutInflater
来获取用户的输入。我在 activity 中单击按钮显示此对话框。这是代码:
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
activity:
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
?
提前致谢。
您可以将数据添加到您的意图中,然后在您新启动的 activity 中检查该数据。
例如:
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..
}
在您的 InputDetail Activity 中创建一个显示对话的方法,
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();
}
});
并且当这个 activity 被调用时,你可以在获得意图后在 OnCreate() 中调用这个方法作为@Hasslam...
@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();
}
}
但是当您从小部件启动此 activity 时,您必须将此布尔值作为...
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)
在一个 activity 中,我使用 AlertDialog.Builder
和 LayoutInflater
来获取用户的输入。我在 activity 中单击按钮显示此对话框。这是代码:
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
activity:
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
?
提前致谢。
您可以将数据添加到您的意图中,然后在您新启动的 activity 中检查该数据。
例如:
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..
}
在您的 InputDetail Activity 中创建一个显示对话的方法,
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();
}
});
并且当这个 activity 被调用时,你可以在获得意图后在 OnCreate() 中调用这个方法作为@Hasslam...
@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();
}
}
但是当您从小部件启动此 activity 时,您必须将此布尔值作为...
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)