Android Dialog 大合集

本文详细介绍Android中各种对话框的创建及使用方法,包括确定取消对话框、多个按钮信息框、列表框、单项选择列表框、进度条框、多项选择列表框、自定义布局及读取进度条等。


1.确定取消对话框

builder.setIcon(R.drawable.icon); 
builder.setTitle("你确定要离开吗?"); 
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { 
public void onClick(DialogInterface dialog, int whichButton) { 
//这里添加点击确定后的逻辑 
showDialog("你选择了确定"); 
} 
}); 
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { 
public void onClick(DialogInterface dialog, int whichButton) { 
//这里添加点击确定后的逻辑 
showDialog("你选择了取消"); 
} 
}); 
builder.create().show(); 


2.多个按钮信息框

AlertDialog.Builder builder = new AlertDialog.Builder(MainDialog.this); 
builder.setIcon(R.drawable.icon); 
builder.setTitle("投票"); 
builder.setMessage("您认为什么样的内容能吸引您?"); 
builder.setPositiveButton("有趣味的", new DialogInterface.OnClickListener() { 
public void onClick(DialogInterface dialog, int whichButton) { 
showDialog("你选择了有趣味的"); 
}
}); 
builder.setNeutralButton("有思想的", new DialogInterface.OnClickListener() { 
public void onClick(DialogInterface dialog, int whichButton) { 
showDialog("你选择了有思想的"); 
} 
}); 
builder.setNegativeButton("主题强的", new DialogInterface.OnClickListener() { 
public void onClick(DialogInterface dialog, int whichButton) { 
showDialog("你选择了主题强的"); 
} 
}); 
builder.create().show(); 


3.列表框

AlertDialog.Builder builder = new AlertDialog.Builder(MainDialog.this); 
builder.setTitle("列表选择框"); 
builder.setItems(mItems, new DialogInterface.OnClickListener() { 
public void onClick(DialogInterface dialog, int which) { 
//点击后弹出窗口选择了第几项 
showDialog("你选择的id为" + which + " , " + mItems[which]); 
} 
}); 
builder.create().show(); 

4.单项选择列表框


mSingleChoice 用于记录单选中的ID

AlertDialog.Builder builder = new AlertDialog.Builder(MainDialog.this); 
mSingleChoiceID = -1; 
builder.setIcon(R.drawable.icon); 
builder.setTitle("单项选择"); 
builder.setSingleChoiceItems(mItems, 0, new DialogInterface.OnClickListener() { 
public void onClick(DialogInterface dialog, int whichButton) { 
mSingleChoiceID = whichButton; 
showDialog("你选择的id为" + whichButton + " , " + mItems[whichButton]); 
} 
}); 
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { 
public void onClick(DialogInterface dialog, int whichButton) { 
if(mSingleChoiceID > 0) { 
showDialog("你选择的是" + mSingleChoiceID); 
} 
} 
}); 
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { 
public void onClick(DialogInterface dialog, int whichButton) { 

} 
}); 
builder.create().show(); 

5.进度条框


点击进度条框按钮后 开启一个线程计算读取的进度 假设读取结束为 100Progress在小于100的时候一直在线程中做循环++ 只到读取结束后,停止线程。

mProgressDialog = new ProgressDialog(MainDialog.this); 
mProgressDialog.setIcon(R.drawable.icon); 
mProgressDialog.setTitle("进度条窗口"); 
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 
mProgressDialog.setMax(MAX_PROGRESS); 
mProgressDialog.setButton("确定", new DialogInterface.OnClickListener() { 
public void onClick(DialogInterface dialog, int whichButton) { 
//这里添加点击后的逻辑 
} 
}); 
mProgressDialog.setButton2("取消", new DialogInterface.OnClickListener() { 
public void onClick(DialogInterface dialog, int whichButton) { 
//这里添加点击后的逻辑 
} 
}); 
mProgressDialog.show(); 
new Thread(this).start(); 

ic void run() { 
int Progress = 0; 
while(Progress < MAX_PROGRESS) { 
try { 
Thread.sleep(100); 
Progress++; 
mProgressDialog.incrementProgressBy(1); 
} catch (InterruptedException e) { 
// TODO Auto-generated catch block 
e.printStackTrace(); 
} 

} 

6.多项选择列表框


MultiChoiceID 用于记录多选选中的id号,存在ArrayList中,选中后 add 进ArrayList,取消选中后 remove 出ArrayList.

ArrayList <Integer>MultiChoiceID = new ArrayList <Integer>(); 

view plaincopy to clipboardprint?
AlertDialog.Builder builder = new AlertDialog.Builder(MainDialog.this); 

MultiChoiceID.clear(); 
builder.setIcon(R.drawable.icon); 
builder.setTitle("多项选择"); 
builder.setMultiChoiceItems(mItems, 
new boolean[]{false, false, false, false, false, false, false}, 
new DialogInterface.OnMultiChoiceClickListener() { 
public void onClick(DialogInterface dialog, int whichButton, 
boolean isChecked) { 
if(isChecked) { 
MultiChoiceID.add(whichButton); 
showDialog("你选择的id为" + whichButton + " , " + mItems[whichButton]); 
}else { 
MultiChoiceID.remove(whichButton); 
} 

} 
}); 
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { 
public void onClick(DialogInterface dialog, int whichButton) { 
String str = ""; 
int size = MultiChoiceID.size(); 
for (int i = 0 ;i < size; i++) { 
str+= mItems[MultiChoiceID.get(i)] + ", "; 
} 
showDialog("你选择的是" + str); 
} 
}); 
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { 
public void onClick(DialogInterface dialog, int whichButton) { 

} 
}); 
builder.create().show(); 

7.自定义布局



讲到自定义布局我就得多说一说了,为什么要多说一说呢? 其实自定义布局在Android的开发中非常重要 因为它能让开发者做出自己五彩缤纷的Activity 而不用去使用系统枯燥的界面。自定义dialog有什么好处?比如我们在开发过长当中 要通过介绍系统发送的一个广播弹出一个dialog . 但是dialog必需是基于activity才能呈现出来 如果没有activity 的话 程序就会崩溃。所以我们可以写一个自定义的 dialog 把它定义成一个activity这样我们收到一条打开dialog的广播后 直接启动这个 activity  程序正常运行~~ 这就是自定义dialog的好处。注明:下面这个例子只是写了自定义dialog 没有把它单独的写在一个activity中 如果须要的话 可以自己改一下。

AlertDialog.Builder builder = new AlertDialog.Builder(MainDialog.this); 
LayoutInflater factory = LayoutInflater.from(this); 
final View textEntryView = factory.inflate(R.layout.test, null); 
builder.setIcon(R.drawable.icon); 
builder.setTitle("自定义输入框"); 
builder.setView(textEntryView); 
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { 
public void onClick(DialogInterface dialog, int whichButton) { 

EditText userName = (EditText) textEntryView.findViewById(R.id.etUserName); 
EditText password = (EditText) textEntryView.findViewById(R.id.etPassWord); 
showDialog("姓名 :" + userName.getText().toString() + "密码:" + password.getText().toString() ); 
} 
}); 
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { 
public void onClick(DialogInterface dialog, int whichButton) { 

} 
}); 
builder.create().show(); 

view plaincopy to clipboardprint?
<span style="color:#000000;"><?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_height="wrap_content" 
android:layout_width="wrap_content" 
android:orientation="horizontal" 
android:id="@+id/dialog"> 
<LinearLayout 
android:layout_height="wrap_content" 
android:layout_width="wrap_content" 
android:orientation="horizontal" 
android:id="@+id/dialogname"> 

<TextView android:layout_height="wrap_content" 
android:layout_width="wrap_content" 
android:id="@+id/tvUserName" 
android:text="姓名:" /> 
<EditText android:layout_height="wrap_content" 
android:layout_width="wrap_content" 
android:id="@+id/etUserName" 
android:minWidth="200dip"/> 
</LinearLayout> 
<LinearLayout 
android:layout_height="wrap_content" 
android:layout_width="wrap_content" 
android:orientation="horizontal" 
android:id="@+id/dialognum" 
android:layout_below="@+id/dialogname" 
> 
<TextView android:layout_height="wrap_content" 
android:layout_width="wrap_content" 
android:id="@+id/tvPassWord" 
android:text="密码:" /> 
<EditText android:layout_height="wrap_content" 
android:layout_width="wrap_content" 
android:id="@+id/etPassWord" 
android:minWidth="200dip"/> 
</LinearLayout> 
</RelativeLayout></span> 

8.读取进度条

显示一个正在转圈的进度条loading

mProgressDialog = new ProgressDialog(this); 
mProgressDialog.setTitle("读取ing"); 
mProgressDialog.setMessage("正在读取中请稍候"); 
mProgressDialog.setIndeterminate(true); 
mProgressDialog.setCancelable(true); 
mProgressDialog.show(); 

内容概要:本研究聚焦于“绿电直连型电氢氨园区”的优化运行,提出一种直接利用绿色电力驱动制氢与合成氨的综合能源系统架构。通过构建包含风/光发电、电解水制氢、氢气储存、合成氨反应及电能直供等关键环节的系统模型,研究旨在实现能源的高效转化与梯级利用,降低对外部电网依赖,提升园区能源自洽率与经济性。研究综合运用Matlab与Python工具进行建模与仿真,结合实际气象与负荷数据,对系统在不同工况下的运行策略、能量流动、设备容量配置及经济技术指标进行深入分析与优化,并形成完整的Word论文文档,为新型零碳产业园区的规划与建设提供了理论依据和技术支撑。; 适合人群:具备新能源、电力系统、化工或综合能源系统背景的科研人员,以及从事园区规划、能源管理、低碳技术开发的工程技术人员。; 使用场景及目标:①研究绿电如何高效耦合至化工生产流程,实现“电-氢-氨”多能互补;②掌握综合能源系统(IES)的建模、仿真与优化方法,特别是多时间尺度下的运行调度策略;③为撰写高水平学术论文或完成相关课题研究积累数据、代码与写作模板。; 阅读建议:此资源包含代码、数据和完整论文,建议使用者先通读Word论文以理解整体框架与理论基础,再结合Matlab/Python代码进行复现与调试,最后可基于提供的数据和模型进行二次开发,以深化对绿电综合利用技术的理解。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值