代码
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>
DiaglogActivity.java
package com.example.myapplication;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.media.Image;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.example.myapplication.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 view) {
switch(view.getId()){
case R.id.btn_dialog1:
LayoutInflater inflater=LayoutInflater.from(DialogActivity.this);
View viewCustom = inflater.inflate(R.layout.layout_list_item,null);
ImageView imageView=viewCustom.findViewById(R.id.iv);
TextView textView1=viewCustom.findViewById(R.id.tv_title2);
TextView textView2=viewCustom.findViewById(R.id.tv_time);
TextView textView3=viewCustom.findViewById(R.id.tv_content);
Glide.with(getApplicationContext()).load("https://i0.hdslb.com/bfs/article/1f2d43c9129520f5e595ab7d5b7eae8fe64b009d.jpg@1320w_778h.webp").into(imageView);
textView1.setText("若爱有颜色");
textView2.setText("那一定是");
textView3.setText("蓝色");
AlertDialog.Builder builder =new AlertDialog.Builder(DialogActivity.this);
builder.setTitle("请回答").setMessage("你觉得蕾姆如何?").setView(viewCustom).setIcon(R.drawable.icon_user)
.setPositiveButton("好", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
ToastUtil.showMsg(DialogActivity.this,"你在想peach,蕾姆是我的");
}
}).setNeutralButton("还行", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
ToastUtil.showMsg(DialogActivity.this,"好好说话");
}
}).setNegativeButton("不太行", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
ToastUtil.showMsg(DialogActivity.this,"闭嘴吧你");
}
}).show();
break;
case R.id.btn_dialog2:
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 dialogInterface, int i) {
ToastUtil.showMsg(DialogActivity.this,array2[i]);
}
}).show();
break;
case R.id.btn_dialog3:
final String[]array3=new String[]{"男","女"};
AlertDialog.Builder builder3=new AlertDialog.Builder(DialogActivity.this);
builder3.setTitle("选择性别").setSingleChoiceItems(array3, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
ToastUtil.showMsg(DialogActivity.this,array3[i]);
dialogInterface.dismiss();
}
}).setCancelable(false).show();
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 dialogInterface, int i, boolean b) {
ToastUtil.showMsg(DialogActivity.this,array4[i]+":"+b);
}
}).setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
}).setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
}).show();
break;
case R.id.btn_dialog5:
final AlertDialog.Builder builder5=new AlertDialog.Builder(DialogActivity.this);
View viewcustom=LayoutInflater.from(DialogActivity.this).inflate(R.layout.layout_dialog,null);
EditText etUserName=viewcustom.findViewById(R.id.et_username);
EditText etPassWord=viewcustom.findViewById(R.id.et_password);
Button btnLogin=viewcustom.findViewById(R.id.btn_login);
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ToastUtil.showMsg(DialogActivity.this,"正在登录中...");
}});
builder5.setTitle("请先登录").setView(viewcustom).show();
break;
}
}
}
}
layout_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:padding="15dp"
android:orientation="vertical">
<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:inputType="textPassword"
android:hint="password"
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:layout_marginTop="10dp"
android:textAllCaps="false"/>
</LinearLayout>