Android - 概述

Android - 警报对话框

对话框是一个小窗口,提示用户做出决定或输入其他信息.

在您的应用程序中,如果您想询问用户在响应用户采取的任何特定操作时做出是或否的决定,通过保持相同的活动而不更改屏幕,您可以使用警报对话框.

In为了建立一个警告对话框,你需要使一个AlertDialogBuilder的对象成为AlertDialog的内部类.其语法如下:

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);

现在你必须使用AlertDialogBuilder类的对象设置正(是)或负(否)按钮.它的语法是

alertDialogBuilder.setPositiveButton(CharSequence text, 
   DialogInterface.OnClickListener listener)
alertDialogBuilder.setNegativeButton(CharSequence text, 
   DialogInterface.OnClickListener listener)

除此之外,您还可以使用构建器类提供的其他功能来自定义警报对话框.这些列在下面

Sr.No方法类型& description
1

setIcon(Drawable icon)

此方法设置警告对话框的图标.

2

setCancelable(boolean cancel able)

此方法设置可以取消对话框的属性

3

setMessage(CharSequence message)

此方法设置要显示的消息警报对话框

4

setMultiChoiceItems(CharSequence [] items,boolean [] checkedItems,DialogInterface.OnMultiChoiceClickListener listener)

此方法设置要在d中显示的项目列表ialog作为内容.所选选项将由听众通知

5

setOnCancelListener(DialogInterface.OnCancelListener onCancelListener)

此方法设置取消对话框时将调用的回调.

6

setTitle(CharSequence title)

此方法将标题设置为出现在对话框中

创建并设置对话框构建器后,您将通过调用构建器类的create()方法创建警报对话框.它的语法是

AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();

这将创建警告对话框并将其显示在屏幕上.

对话框片段

在进入一个例子之前我们应该知道对话框片段.Dialog片段是一个可以在对话框中显示片段的片段

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.

要试验这个例子,你需要在模拟器或实际设备上运行它.

步骤描述
1您将使用Android studio创建Android应用程序并命名作为我的应用程序包com.example.sairamkrishna.myapplication.
2修改src/MainActivity.java文件以添加警告对话框代码以启动对话框.
3修改布局XML文件res/layout/activity_main.xml如果需要,添加任何GUI组件.
4否n eed更改默认字符串常量. Android studio负责处理值/string.xml的默认字符串
5运行应用程序并选择正在运行的Android设备并在其上安装应用程序并验证结果.

以下是 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 表示it1352的标识.com
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
   xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
   android:layout_height="match_parent" 
   android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   android:paddingBottom="@dimen/activity_vertical_margin" 
   tools:context=".MainActivity">
   
   <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Alert Dialog"
      android:id="@+id/textView"
      android:textSize="35dp"
      android:layout_alignParentTop="true"
      android:layout_centerHorizontal="true" />
      
   <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Tutorialspoint"
      android:id="@+id/textView2"
      android:textColor="#ff3eff0f"
      android:textSize="35dp"
      android:layout_below="@+id/textView"
      android:layout_centerHorizontal="true" />
      
   <ImageView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/imageView"
      android:src="@drawable/abc"
      android:layout_below="@+id/textView2"
      android:layout_alignRight="@+id/textView2"
      android:layout_alignEnd="@+id/textView2"
      android:layout_alignLeft="@+id/textView"
      android:layout_alignStart="@+id/textView" />
   <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Alert dialog"
      android:id="@+id/button"
      android:layout_below="@+id/imageView"
      android:layout_alignRight="@+id/textView2"
      android:layout_alignEnd="@+id/textView2"
      android:layout_marginTop="42dp"
      android:onClick="open"
      android:layout_alignLeft="@+id/imageView"
      android:layout_alignStart="@+id/imageView" />
      
</RelativeLayout>

这是 Strings.xml

<resources>
    <string name="app_name">My Application</string>
</resources>

以下是 AndroidManifest.xml的默认代码

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.sairamkrishna.myapplication" >

   <application
      android:allowBackup="true"
      android:icon="@drawable/ic_launcher"
      android:label="@string/app_name"
      android:theme="@style/AppTheme" >
      
      <activity
         android:name="com.example.sairamkrishna.myapplication.MainActivity"
         android:label="@string/app_name" >
         
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
         
      </activity>
      
   </application>
</manifest>

让我们尝试运行您的应用程序.我假设您已将实际的Android移动设备与计算机相关联.要从Android工作室运行应用程序,请打开项目的一个活动文件,然后单击运行Eclipse Run Icon icon从工具栏中.在开始申请之前,] Android工作室将显示以下窗口,以选择您要运行Android应用程序的选项.

Anroid Camera Tutorial

选择一个选项,然后单击它.假设,如果你点击了yes按钮,那么结果如下

Anroid Camera Tutorial

如果你点击no按钮就会调用finish(),它会关闭你的应用程序.