Android对话框Dialog的使用
程序员文章站
2022-06-25 09:04:00
Dialog的使用方法...
普通对话框
private void showAlertDialogShow(){
AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
alertDialog.setIcon(R.mipmap.ic_launcher);
alertDialog.setTitle("确定选择?");
alertDialog.setMessage("........");
alertDialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,"三连支持",Toast.LENGTH_SHORT).show();
}
});
alertDialog.setNegativeButton("拒绝", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,"下次一定",Toast.LENGTH_SHORT).show();
}
});
alertDialog.setNeutralButton("忽视", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,"假装看不见",Toast.LENGTH_SHORT).show();
}
});
alertDialog.show();
}
列表对话框
private void showListDialogShow(){
AlertDialog.Builder listDialog = new AlertDialog.Builder(MainActivity.this);
listDialog.setTitle("谁是最强的男人?");
listDialog.setIcon(R.mipmap.ic_launcher);
final String items[] = {"A、折木奉太郎","B、空调承太郎","C、野比大雄"};
listDialog.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,items[which],Toast.LENGTH_SHORT).show();
}
});
listDialog.show();
}
单选对话框
int choice;
private void showSingleChoiceDialogShow(){
final String items[] = {"A、折木奉太郎","B、空调承太郎","C、野比大雄"};
AlertDialog.Builder singleDialog = new AlertDialog.Builder(MainActivity.this);
singleDialog.setTitle("谁是最强的男人?");
singleDialog.setIcon(R.mipmap.ic_launcher);
//参数2为默认选项
singleDialog.setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
choice = which;
}
});
singleDialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,items[choice],Toast.LENGTH_SHORT).show();
}
});
singleDialog.show();
}
多选对话框
ArrayList<Integer> choices= new ArrayList<>();
private void showMultiDialogShow(){
choices.clear();
final boolean isChecked[] = {false,false,false};
final String items[] = {"A、折木奉太郎","B、空调承太郎","C、野比大雄"};
AlertDialog.Builder multiDialog = new AlertDialog.Builder(MainActivity.this);
multiDialog.setTitle("谁是最强的男人?");
multiDialog.setIcon(R.mipmap.ic_launcher);
multiDialog.setMultiChoiceItems(items, isChecked, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
if(isChecked){
choices.add(which);
}else{
choices.remove(which);
}
}
});
multiDialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String s="";
for (Integer choice : choices) {
s = s+items[choice];
}
Toast.makeText(MainActivity.this,s,Toast.LENGTH_SHORT).show();
}
});
multiDialog.show();
}
参考文章
本文地址:https://blog.csdn.net/weixin_43742354/article/details/103945159
推荐阅读
-
Android使用Gallery实现照片拖动的特效
-
详解Android aidl的使用方法
-
Easyui使用Dialog行内按钮布局的实例
-
Android Retrofit2+rxjava2+Mvp基于okhttp3网络请求框架的使用 一 框架配置
-
浅谈Android注解在日常开发中的简单使用
-
Android Handler的使用详解
-
Android bindService的使用与Service生命周期案例详解
-
Android-轮播图banner的使用步骤
-
Android使用AudioRecord和AudioTrack完成音频的采集和播放以及使用MediaCodec完成硬编和硬解
-
Android获取当前应用分配的最大内存和目前使用内存的方法