欢迎来到代码驿站!

Android代码

当前位置:首页 > 移动开发 > Android代码

Android中AlertDialog四种对话框的最科学编写用法(实例代码)

时间:2020-10-16 12:40:55|栏目:Android代码|点击:

首先我们上图:

 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 1.5 1.6 2.0 2.1 2.2 的区别详解

栏    目:Android代码

下一篇:Android中TextView实现部分文字可点击跳转

本文标题:Android中AlertDialog四种对话框的最科学编写用法(实例代码)

本文地址:http://www.codeinn.net/misctech/12201.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有