AlertDialog的几种用法
xml代码:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.lesson7_3_id19_alertdialog.MainActivity"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="简单的dialog" android:onClick="dialog_1"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="列表的dialog" android:onClick="dialog_2"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="单选的dialog" android:onClick="dialog_3"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="多选的dialog" android:onClick="dialog_4"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="自定义View的dialog" android:onClick="dialog_5"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="使用adapter的dialog" android:onClick="dialog_6"/> </LinearLayout>
java代码:
package com.example.lesson7_3_id19_alertdialog; import android.app.AlertDialog; import android.content.DialogInterface; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.ArrayAdapter; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void dialog_1(View v){ AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setIcon(R.mipmap.ic_launcher_round); 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("取消",null); builder.setNeutralButton("中立",null); AlertDialog alertDialog = builder.create(); alertDialog.show(); } private String [] item = {"游戏","运动","电影","旅游","看书"}; public void dialog_2(View v){ AlertDialog.Builder builder = new AlertDialog.Builder(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(); } int index; public void dialog_3(View v){ AlertDialog.Builder builder = new AlertDialog.Builder(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(); } // 设置boolean数组所有的选项设置默认没选 boolean[] bools = {false,false,false,false,false}; public void dialog_4(View v){ AlertDialog.Builder builder = new AlertDialog.Builder(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(); } public void dialog_5(View v){ AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("连接wifi"); final EditText et = new EditText(this); et.setHint("请输入密码"); et.setSingleLine(true); builder.setView(et); builder.setNegativeButton("取消",null); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { String password = et.getText().toString(); if (password.equals("123456")) { Toast.makeText(MainActivity.this, "连接成功", Toast.LENGTH_SHORT).show(); }else{ Toast.makeText(MainActivity.this, "密码错误", Toast.LENGTH_SHORT).show(); } } }); AlertDialog alertDialog = builder.create(); alertDialog.show(); } public void dialog_6(View v){ ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,item); AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("使用适配器"); builder.setAdapter(adapter, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "选择了"+item[which], Toast.LENGTH_SHORT).show(); } }); AlertDialog alertDialog = builder.create(); alertDialog.show(); } }
到此这篇关于Android AlertDialog的几种用法详解的文章就介绍到这了,更多相关Android AlertDialog方法内容请搜索自学编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持自学编程网!
- 本文固定链接: https://zxbcw.cn/post/219885/
- 转载请注明:必须在正文中标注并保留原文链接
- QQ群: PHP高手阵营官方总群(344148542)
- QQ群: Yii2.0开发(304864863)