怎么在Android中使用AlertDialog实现对话框

发布时间:2021-05-26 09:36:18 作者:Leah
来源:亿速云 阅读:158

怎么在Android中使用AlertDialog实现对话框?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

 xml的代码如下,用于编写按钮:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
xmlns:widget="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">
  <Button
    android:id="@+id/button_1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="简单的dialog"
    />
  <Button
    android:id="@+id/button_2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="列表的dialog"
    />
  <Button
    android:id="@+id/button_3"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="单选的dialog"
    />
  <Button
    android:id="@+id/button_4"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="多选的dialog"
    />
</LinearLayout>

Java代码如下,用于实现逻辑:

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity{
  int index;
  String [] item = {"Android","IOS","Spark","Hadoop","Web"};
  boolean[] bools = {false,false,false,false,false};
  // 设置boolean数组所有的选项设置默认没选
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ActionBar actionBar = getSupportActionBar();
    if (actionBar != null) {
      actionBar.hide();
    }
    Button button=(Button)findViewById(R.id.button_1);
    button.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        builder.setIcon(R.drawable.girl);
        builder.setTitle("标题栏");
        builder.setMessage("对话框内容,可自行设置");
        builder.setPositiveButton("确定",new DialogInterface.OnClickListener() {
          @Override
          public void onClick(DialogInterface dialog, int which) {
            Toast.makeText(MainActivity.this, "点击了确定", Toast.LENGTH_SHORT).show();
          }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
          @Override
          public void onClick(DialogInterface dialogInterface, int i) {
            Toast.makeText(MainActivity.this, "点击了取消", Toast.LENGTH_SHORT).show();
          }
        });
        builder.setNeutralButton("好的", new DialogInterface.OnClickListener() {
          @Override
          public void onClick(DialogInterface dialogInterface, int i) {
            Toast.makeText(MainActivity.this, "点击了“好的”", Toast.LENGTH_SHORT).show();
          }
        });
        AlertDialog alertDialog = builder.create();
        alertDialog.show();
      }
    });
    Button button2=(Button)findViewById(R.id.button_2);
    button2.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        builder.setTitle("请选择一个技术分支");
        builder.setItems(item, new DialogInterface.OnClickListener() {
          @Override
          public void onClick(DialogInterface dialog, int which) {
            Toast.makeText(MainActivity.this, "选择了"+item[which], Toast.LENGTH_SHORT).show();
          }
        });
        // 取消可以不添加
        //builder.setNegativeButton("取消",null);
        AlertDialog alertDialog = builder.create();
        alertDialog.show();
      }
    });
    Button button3=(Button)findViewById(R.id.button_3);
    button3.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        builder.setTitle("请选择技术分支:");
        builder.setSingleChoiceItems(item, index, new DialogInterface.OnClickListener() {
          @Override
          public void onClick(DialogInterface dialog, int which) {
            index = which;
          }
        });
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
          @Override
          public void onClick(DialogInterface dialog, int which) {
            Toast.makeText(MainActivity.this, "选择了"+item[index], Toast.LENGTH_SHORT).show();
          }
        });
        builder.setNegativeButton("取消",null);
        AlertDialog alertDialog = builder.create();
        alertDialog.show();
      }
    });
    Button button4=(Button)findViewById(R.id.button_4);
    button4.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        builder.setTitle("请选择技术分支:");
        builder.setMultiChoiceItems(item, bools, new DialogInterface.OnMultiChoiceClickListener() {
          @Override
          public void onClick(DialogInterface dialog, int which, boolean isChecked) {
            bools[which] = isChecked;
          }
        });
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
          @Override
          public void onClick(DialogInterface dialog, int which) {
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < item.length; i++) {
              if (bools[i]) {
                sb.append(item[i] + " ");
              }
            }
            Toast.makeText(MainActivity.this, "选择了" + sb.toString(), Toast.LENGTH_SHORT).show();
          }
        });
        builder.setNegativeButton("取消",null);
        AlertDialog alertDialog = builder.create();
        alertDialog.show();
      }
    });
  }
}

Android是什么

Android是一种基于Linux内核的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由美国Google公司和开放手机联盟领导及开发。

看完上述内容,你们掌握怎么在Android中使用AlertDialog实现对话框的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

推荐阅读:
  1. 常见对话框AlertDialog如何理解
  2. Android AlertDialog对话框回调

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

android alertdialog

上一篇:怎么在Spring Data Jpa中使用模糊查询

下一篇:如何在Asp.Net Core中使用生命周期选项

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》