这次示例是对话框的使用教程:
布局界面如下:
定义五个按钮对应五个布局,这里的自定义布局可以按照自己意愿定义。
Activity:
package com.example.duihuakuang;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.app.ProgressDialog;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button btn_pt;
private Button btn_dx;
private Button btn_dx2;
private Button btn_jd;
private Button btn_zdy;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
inint();
}
public void inint()
{
btn_pt=findViewById(R.id.btn_pt);
btn_dx=findViewById(R.id.btn_dx);
btn_dx2=findViewById(R.id.btn_dx2);
btn_jd=findViewById(R.id.btn_jd);
btn_zdy=findViewById(R.id.btn_zdy);
btn_pt.setOnClickListener(this);
btn_dx.setOnClickListener(this);
btn_dx2.setOnClickListener(this);
btn_jd.setOnClickListener(this);
btn_zdy.setOnClickListener(this);
}
public void onClick(View v)
{
switch (v.getId())
{
case R.id.btn_pt:
pt();
break;
case R.id.btn_dx:
dx();
break;
case R.id.btn_dx2:
dx2();
break;
case R.id.btn_jd:
jd();
break;
case R.id.btn_zdy:
break;
}
}
public void pt()
{ //声明对象
AlertDialog dialog;
dialog=new AlertDialog.Builder(MainActivity.this).setTitle("Dialog--普通对话框")
.setMessage("是否确认退出")//设置提示信息
.setIcon(R.drawable.ic_launcher_foreground)//设置图标
.setPositiveButton("确认",null)//添加确定按钮
.setNegativeButton("取消",null)
.create();//创建对话框
dialog.show();//显示对话框
}
public void dx()
{
new AlertDialog.Builder(MainActivity.this)
.setTitle("请选择性别")
.setIcon(R.drawable.ic_launcher_foreground)
.setSingleChoiceItems(new String[]{"男", "女"}, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.setPositiveButton("确认",null)
.show();
}
public void dx2()
{
new AlertDialog.Builder(MainActivity.this)
.setTitle("请添加爱好")
.setIcon(R.drawable.ic_launcher_foreground)
.setMultiChoiceItems(new String[]{"路游","打游戏","看电影","唱歌"},null,null)
.setPositiveButton("确定",null)
.show();
}
public void jd(){
ProgressDialog jd;//声明对话框
jd=new ProgressDialog(MainActivity.this);//构建对话框
jd.setTitle("进度条对话框");
jd.setIcon(R.mipmap.ic_launcher);
jd.setMessage("正在下载请等候");
jd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
jd.show();
//ProgressDialog.STYLE_HORIZONTAL水平进度条
//ProgressDialog.STYLE_SPINNER垂直进度条
}
}
实现效果: