Android学习笔记3-2
推荐新手向学习视频:B站https://www.bilibili.com/video/av38409964点我传送
3-2 AlertDialog
-
activity_dialog.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:orientation="vertical"> <Button android:id="@+id/btn_dialog1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="style1" android:textAllCaps="false"/> <Button android:id="@+id/btn_dialog2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="style2" android:textAllCaps="false"/> <Button android:id="@+id/btn_dialog3" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="style3" android:textAllCaps="false"/> <Button android:id="@+id/btn_dialog4" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="style4" android:textAllCaps="false"/> <Button android:id="@+id/btn_dialog5" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="style5" android:textAllCaps="false"/> </LinearLayout>
-
效果
-
layout_dialog.xml文件,自定义布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="15dp"> <EditText android:id="@+id/et_username" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="username" android:maxLines="1"/> <EditText android:id="@+id/et_password" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="password" android:inputType="textPassword" android:maxLines="1" android:layout_marginTop="10dp"/> <Button android:id="@+id/btn_login" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="login" android:textAllCaps="false" android:layout_marginTop="10dp"/> </LinearLayout>
-
效果
-
DialogActivity.java文件
package com.ylw.helloworld; import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AppCompatActivity; import android.content.DialogInterface; import android.content.Intent; import android.os.Bundle; import android.text.Layout; import android.view.LayoutInflater; import android.view.View; import android.widget.Button; import android.widget.EditText; import com.ylw.helloworld.util.ToastUtil; public class DialogActivity extends AppCompatActivity { private Button mBtnDialog1,mBtnDialog2,mBtnDialog3,mBtnDialog4,mBtnDialog5; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_dialog); mBtnDialog1 = findViewById(R.id.btn_dialog1); mBtnDialog2 = findViewById(R.id.btn_dialog2); mBtnDialog3 = findViewById(R.id.btn_dialog3); mBtnDialog4 = findViewById(R.id.btn_dialog4); mBtnDialog5 = findViewById(R.id.btn_dialog5); OnClick onClick = new OnClick(); mBtnDialog1.setOnClickListener(onClick); mBtnDialog2.setOnClickListener(onClick); mBtnDialog3.setOnClickListener(onClick); mBtnDialog4.setOnClickListener(onClick); mBtnDialog5.setOnClickListener(onClick); } class OnClick implements View.OnClickListener{ @Override public void onClick(View v) { switch (v.getId()){ case R.id.btn_dialog1: //默认样式 AlertDialog.Builder builder = new AlertDialog.Builder(DialogActivity.this); //builder.setTitle("请回答"); //builder.setMessage("你想学习Android吗?"); //还可以用链式,因为返回值也是builder builder.setTitle("请回答").setMessage("你想学习Android吗?") .setIcon(R.drawable.icon_user) .setPositiveButton("想", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { ToastUtil.showMsg(DialogActivity.this,"不,你不想"); } }).setNeutralButton("还行", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { ToastUtil.showMsg(DialogActivity.this,"你再想想"); } }).setNegativeButton("不想", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { ToastUtil.showMsg(DialogActivity.this,"你很诚实"); } }).show(); break; case R.id.btn_dialog2: //单选样式1 final String [] array2 = new String[]{"男","女"}; AlertDialog.Builder builder2 = new AlertDialog.Builder(DialogActivity.this); builder2.setTitle("选择性别").setItems(array2, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { ToastUtil.showMsg(DialogActivity.this,array2[which]); } }).show(); break; case R.id.btn_dialog3: //单选样式2 final String [] array3 = new String[]{"男","女"}; AlertDialog.Builder builder3 = new AlertDialog.Builder(DialogActivity.this); //setSingleChoiceItems(数组,默认选中的数组索引,监听) builder3.setTitle("选择性别").setSingleChoiceItems(array3, 0, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { ToastUtil.showMsg(DialogActivity.this,array3[which]); dialog.dismiss(); //点击选项时对话框消失 } }).setCancelable(false).show(); //setCancelable(false)点击其他部分不会关闭对话框,配合dialog.dismiss()使用 break; case R.id.btn_dialog4: //多选 final String[] array4 = new String[]{"唱歌","跳舞","写代码"}; boolean[] isSelected = new boolean[]{false,false,true}; AlertDialog.Builder builder4 = new AlertDialog.Builder(DialogActivity.this); builder4.setTitle("选择兴趣").setMultiChoiceItems(array4, isSelected, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { ToastUtil.showMsg(DialogActivity.this,array4[which]+":"+isChecked); } }).setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // } }).setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // } }).show(); break; case R.id.btn_dialog5: //自定义布局 AlertDialog.Builder builder5 = new AlertDialog.Builder(DialogActivity.this); View view = LayoutInflater.from(DialogActivity.this).inflate(R.layout.layout_dialog,null); EditText etUserName = view.findViewById(R.id.et_username); EditText etPassWord = view.findViewById(R.id.et_password); Button btnLogin = view.findViewById(R.id.btn_login); btnLogin.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // } }); builder5.setTitle("请先登录").setView(view).show(); break; } } } }
-
默认样式
-
单选样式1
-
单选样式2
-
多选样式
-
自定义样式
来源:CSDN
作者:影龙武
链接:https://blog.csdn.net/qq_43594119/article/details/104576210