栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

【Android】UI组件之AlertDialog

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【Android】UI组件之AlertDialog

1、对话框定义

对话框是程序运行中的弹出窗口,例如,当用户要删除一个联系方式时,会弹出一个对话框,让用户确认是否真的要删除。

Android系统提供了多种对话框:

  • AlertDialog(警告对话框)
  • ProgressDialog(进度条对话框)
  • DatePickerDialog(日期选择对话框)
  • TimerPickerDialog(时间选择对话框)
2、AlertDialog创建方式
  • title 标题是可选部分
  • Content Area 显示message、list…
  • Action Buttons 可以添加操作,不超过三个操作按钮
package com.example.test_alertdialog;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void test1(View view){
        //如何创建一个alertdialog
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("提示信息!");
        builder.setMessage("我的第一个对话框!");

        AlertDialog alertDialog = builder.create();
        alertDialog.show();
    }
}

3、对话框添加按钮

包括不同性质的按钮:setPositiveButton、setNegativeButton、setNeutralButton

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void test1(View view){
        //如何创建一个alertdialog
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("提示信息!");
        builder.setMessage("我的第一个对话框!");

        builder.setPositiveButton("非常满意", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                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();
    }
}


4、对话框点击显示
public void test2(View view){
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("提示信息!");
        builder.setItems(ss, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                Toast.makeText(MainActivity.this, ss[i], Toast.LENGTH_SHORT).show();
            }
        });
        AlertDialog alertDialog = builder.create();
        alertDialog.show();
    }
 

5、对话框单项选择

解析:

builder.setSingleChoiceItems(ss, **0**, new DialogInterface.OnClickListener(){}
//其中的0,表示初始点开时选中数组中第0个

代码:

public void test3(View view){
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("提示信息!");

        builder.setSingleChoiceItems(ss, 0, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                Toast.makeText(MainActivity.this, ss[i], Toast.LENGTH_SHORT).show();
            }
        });
        AlertDialog alertDialog = builder.create();
        alertDialog.show();
    }
 

6、对话框多项选择

解析:

builder.setMultiChoiceItems(ss, null, new DialogInterface.OnMultiChoiceClickListener() {}
 //其中的null,表示初始点开时不选中数组中的任意值
 //null也可换成new boolean[]{true,false,true},true为选中,false为不选中

代码:

public void test4(View view){
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("提示信息!");

        builder.setMultiChoiceItems(ss, null, new DialogInterface.OnMultiChoiceClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i, boolean b) {
                Toast.makeText(MainActivity.this, ss[i], Toast.LENGTH_SHORT).show();
            }
        });
        AlertDialog alertDialog = builder.create();
        alertDialog.show();
    }

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1038995.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号