Android学习笔记3-2 推荐新手向学习视频:B站https://www.bilibili.com/video/av38409964点我传
activity_dialog.xml文件
效果
效果
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
多选样式
自定义样式
--结束END--
本文标题: Android学习笔记(Android Studio)3-2(AlertDialog)UI组件之弹出组件
本文链接: https://lsjlt.com/news/28901.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-01-21
2023-10-28
2023-10-28
2023-10-27
2023-10-27
2023-10-27
2023-10-27
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0