如何创建带有中性选项的对话框?

androidmobile developmentapps/applications

此示例演示了如何创建带有中性选项的对话框。

步骤 1 − 在 Android Studio 中创建一个新项目,转到"文件"->"新建项目",并填写所有必需的详细信息以创建新项目。

步骤 2 − 将以下代码添加到 res/layout/activity_main.xml。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/parent"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   tools:context=".MainActivity"
   android:gravity="center"
   android:orientation="vertical">
   <Button
      android:id="@+id/customDialog"
      android:text="Custom Dialog"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" />
</LinearLayout>

在上面的代码中,我们获取了按钮。当用户点击按钮时,它将显示对话框。

步骤 3 − 将以下代码添加到 src/MainActivity.java

package com.example.andy.myapplication;
import android.content.DialogInterface;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
   @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      findViewById(R.id.customDialog).setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this, R.style.CustomAlertDialog);
            builder.setTitle("Alert Dialog");
            builder.setMessage("Lorem Ipsum is simply dummy text of the printing and typesetting industry.
               Lorem Ipsum has been the industry's standard dummy text ever since the 1500s");
            builder.setPositiveButton("yes", new DialogInterface.OnClickListener() {
               @Override
               public void onClick(DialogInterface dialog, int which) {
                  Toast.makeText(MainActivity.this, "You have clicked yes", Toast.LENGTH_LONG).show();
               }
            });
            builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
               @Override
               public void onClick(DialogInterface dialog, int which) {
                  Toast.makeText(MainActivity.this, "You have clicked No", Toast.LENGTH_LONG).show();
               }
            });
            builder.setNeutralButton("Cancel", new DialogInterface.OnClickListener() {
               @Override
               public void onClick(DialogInterface dialog, int which) {
                  Toast.makeText(MainActivity.this, "You have clicked Neutral", Toast.LENGTH_LONG).show();
               }
            });
            final AlertDialog alertDialog = builder.create();
            alertDialog.show();
         }
      });
   }
}

在上面的代码中,当用户点击按钮时,会弹出包含"是"、"中性"和"否"按钮的对话框。点击"是"按钮时,会显示"是"消息,"否"按钮显示"无"消息,"中性"按钮显示"中性"消息。

让我们尝试运行您的应用程序。假设您已将实际的 Android 移动设备连接到电脑。要从 Android Studio 运行该应用程序,请打开项目的某个活动文件,然后点击工具栏上的"运行"图标  。选择您的移动设备作为选项,然后检查您的移动设备是否显示您的默认屏幕 −

以上结果显示了初始屏幕。现在点击按钮,它将打开一个对话框,其中包含"是"、"否"和"中性"按钮。

现在点击"中性"按钮,它将显示如下所示的输出 -

点击此处下载项目代码


相关文章