📅  最后修改于: 2021-01-05 05:04:54             🧑  作者: Mango
对话框是一个小窗口,提示用户做出决定或输入其他信息。
在您的应用程序中,有时候,如果您想让用户根据用户采取的任何特定操作,在是或否之间做出决定,方法是保持相同的活动并且不更改屏幕,则可以使用“警告对话框”。
为了创建警报对话框,您需要创建一个AlertDialogBuilder对象,该对象是AlertDialog的内部类。其语法如下
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
现在,您必须使用AlertDialogBuilder类的对象设置正(是)或负(否)按钮。它的语法是
alertDialogBuilder.setPositiveButton(CharSequence text,
DialogInterface.OnClickListener listener)
alertDialogBuilder.setNegativeButton(CharSequence text,
DialogInterface.OnClickListener listener)
除此之外,您还可以使用builder类提供的其他功能来自定义警报对话框。这些在下面列出
Sr.No | Method type & description |
---|---|
1 |
setIcon(Drawable icon) This method set the icon of the alert dialog box. |
2 |
setCancelable(boolean cancel able) This method sets the property that the dialog can be cancelled or not |
3 |
setMessage(CharSequence message) This method sets the message to be displayed in the alert dialog |
4 |
setMultiChoiceItems(CharSequence[] items, boolean[] checkedItems, DialogInterface.OnMultiChoiceClickListener listener) This method sets list of items to be displayed in the dialog as the content. The selected option will be notified by the listener |
5 |
setOnCancelListener(DialogInterface.OnCancelListener onCancelListener) This method Sets the callback that will be called if the dialog is cancelled. |
6 |
setTitle(CharSequence title) This method set the title to be appear in the dialog |
创建并设置对话框构建器之后,您将通过调用构建器类的create()方法来创建警报对话框。它的语法是
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
这将创建警报对话框,并将其显示在屏幕上。
在进入示例之前,我们需要了解对话框片段。对话框片段是可以在对话框中显示片段的片段
public class DialogFragment extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setPositiveButton(R.string.fire, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
toast.makeText(this,"enter a text here",Toast.LENTH_SHORT).show();
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
finish();
});
// Create the AlertDialog object and return it
return builder.create();
}
}
}
它用于在对话框中显示项目列表。假设用户需要选择一个项目列表,或者需要从多个项目列表中单击一个项目,在这种情况下,我们可以使用列表对话框。
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(Pick a Color)
.setItems(R.array.colors_array, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// The 'which' argument contains the index position
// of the selected item
}
});
return builder.create();
}
它曾经用来在对话框中添加单选列表。我们可以根据用户的选择进行选择或取消选择。
public Dialog onCreateDialog(Bundle savedInstanceState) {
mSelectedItems = new ArrayList();
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("This is list choice dialog box");
.setMultiChoiceItems(R.array.toppings, null,
new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
if (isChecked) {
// If the user checked the item, add it to the selected items
mSelectedItems.add(which);
}
else if (mSelectedItems.contains(which)) {
// Else, if the item is already in the array, remove it
mSelectedItems.remove(Integer.valueOf(which));
}
}
})
// Set the action buttons
.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
// User clicked OK, so save the mSelectedItems results somewhere
// or return them to the component that opened the dialog
...
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
...
}
});
return builder.create();
}
以下示例演示了在Android中使用AlertDialog的方法。
要试验该示例,您需要在仿真器或实际设备上运行它。
Steps | Description |
---|---|
1 | You will use Android studio to create an Android application and name it as My Application under a package com.example.sairamkrishna.myapplication. |
2 | Modify src/MainActivity.java file to add alert dialog code to launch the dialog. |
3 | Modify layout XML file res/layout/activity_main.xml add any GUI component if required. |
4 | No need to change default string constants. Android studio takes care of default strings at values/string.xml |
5 | Run the application and choose a running android device and install the application on it and verify the results. |
这是src / MainActivity.java的修改后的代码
package com.example.sairamkrishna.myapplication;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void open(View view){
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setMessage("Are you sure,
You wanted to make decision");
alertDialogBuilder.setPositiveButton("yes",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
Toast.makeText(MainActivity.this,"You clicked yes
button",Toast.LENGTH_LONG).show();
}
});
alertDialogBuilder.setNegativeButton("No",new DialogInterface.OnClickListener() {
Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
}
这是res / layout / activity_main.xml的修改后的代码
在下面的代码中abc表示tutorialspoint.com的徽标
这是Strings.xml
My Application
这是AndroidManifest.xml的默认代码
让我们尝试运行您的应用程序。我假设您已将实际的Android Mobile设备与计算机连接。要从Android Studio运行该应用,请打开您项目的活动文件之一,然后点击运行工具栏中的图标。在启动应用程序之前,] Android studio将显示以下窗口,以选择要在其中运行Android应用程序的选项。
选择一个选项,然后单击它。假设,如果您单击“是”按钮,则结果如下
如果您单击无按钮,它将调用finish()并关闭您的应用程序。