Andriod 对话框
来源:互联网 发布:vb中ucase是什么意思 编辑:程序博客网 时间:2024/05/18 02:07
在Activity中可以调用showDialog()来显示一个对话框,覆盖Activity的onCreateDialog方法,在这个方法中创建对话框,返回一个Dialog对象。
1.最简单的对话框
- AlertDialog.Builder b=new AlertDialog.Builder(this);
- b.setTitle("简单的");
- b.setMessage("this is a simple dialog");
- b.setPositiveButton("是", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // TODO Auto-generated method stub
- }
- });
- b.setNegativeButton("否", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // TODO Auto-generated method stub
- }
- });
- return b.create();
AlertDialog.Builder b=new AlertDialog.Builder(this);b.setTitle("简单的");b.setMessage("this is a simple dialog");b.setPositiveButton("是", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stub}});b.setNegativeButton("否", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stub}});return b.create();
效果如下
2.列表对话框
- b.setTitle("列表");
- //b.setMessage("message");这行代码不要有
- b.setItems(items, new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // TODO Auto-generated method stub
- Toast.makeText(AndroidDialogActivity.this, items[which], Toast.LENGTH_SHORT).show();
- }
- });
- return b.create();
b.setTitle("列表");//b.setMessage("message");这行代码不要有b.setItems(items, new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stubToast.makeText(AndroidDialogActivity.this, items[which], Toast.LENGTH_SHORT).show();}});return b.create();
items是一个String数组
效果图
3.单选对话框
- b.setTitle("请选择颜色");
- b.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // TODO Auto-generated method stub
- Toast.makeText(AndroidDialogActivity.this, items[which], Toast.LENGTH_SHORT).show();
- }
- });
- b.setPositiveButton("是", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // TODO Auto-generated method stub
- }
- });
- b.setNegativeButton("否", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // TODO Auto-generated method stub
- }
- });
- return b.create();
b.setTitle("请选择颜色");b.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stubToast.makeText(AndroidDialogActivity.this, items[which], Toast.LENGTH_SHORT).show();}});b.setPositiveButton("是", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stub}});b.setNegativeButton("否", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stub}});return b.create();
效果图
4.多选对话框
- boolean []ddd=new boolean[3];
- b.setTitle("请选择颜色");
- b.setMultiChoiceItems(items, ddd, new DialogInterface.OnMultiChoiceClickListener(){
- @Override
- public void onClick(DialogInterface dialog, int which,
- boolean isChecked) {
- // TODO Auto-generated method stub
- }
- });
- return b.create();
boolean []ddd=new boolean[3];b.setTitle("请选择颜色");b.setMultiChoiceItems(items, ddd, new DialogInterface.OnMultiChoiceClickListener(){@Overridepublic void onClick(DialogInterface dialog, int which,boolean isChecked) {// TODO Auto-generated method stub}});return b.create();
效果图
5.进度条对话框
- Handler hand=new Handler(){
- @Override
- public void handleMessage(Message msg) {
- // TODO Auto-generated method stub
- super.handleMessage(msg);
- if(progressint>=100)
- {
- pd.dismiss();
- }
- else
- {
- progressint++;
- pd.setProgress(progressint);
- hand.sendEmptyMessageDelayed(0, 100);
- }
- }
- };
- pd=new ProgressDialog(this);
- pd.setTitle("进度对话框");
- pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
- pd.setMax(100);
- AndroidDialogActivity.this.hand.sendEmptyMessage(0);
- pd.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener(){
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // TODO Auto-generated method stub
- }});
- return pd;
Handler hand=new Handler(){@Overridepublic void handleMessage(Message msg) {// TODO Auto-generated method stubsuper.handleMessage(msg);if(progressint>=100){pd.dismiss();}else{progressint++;pd.setProgress(progressint);hand.sendEmptyMessageDelayed(0, 100);}}};pd=new ProgressDialog(this);pd.setTitle("进度对话框");pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);pd.setMax(100);AndroidDialogActivity.this.hand.sendEmptyMessage(0);pd.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener(){@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stub}});return pd;
效果图
6.代码自定义对话框
- EditText et=new EditText(this);
- et.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD|InputType.TYPE_CLASS_TEXT);
- b.setTitle("请输入密码");
- b.setView(et);
- return b.create();
EditText et=new EditText(this);et.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD|InputType.TYPE_CLASS_TEXT);b.setTitle("请输入密码");b.setView(et);return b.create();
效果图
7.XML文件自定义对话框
xml文件
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
- <TextView
- android:id="@+id/textView1"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="用户名" />
- <EditText
- android:id="@+id/EditText1"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="TextView"
- />
- <TextView
- android:id="@+id/textView2"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="密码" />
- <EditText
- android:id="@+id/EdiText2"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="TextView"
- android:inputType="textPassword"
- />
- <Button
- android:id="@+id/buttonyes"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="确定" />
- </LinearLayout>
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/textView1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="用户名" /> <EditText android:id="@+id/EditText1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="TextView" /> <TextView android:id="@+id/textView2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="密码" /> <EditText android:id="@+id/EdiText2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="TextView" android:inputType="textPassword" /> <Button android:id="@+id/buttonyes" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="确定" /></LinearLayout>
Java代码
- b.setIcon(R.drawable.ic_launcher);
- b.setTitle("自定义对话框");
- LayoutInflater li=LayoutInflater.from(this);
- View v=li.inflate(R.layout.info, null);
- Button yes=(Button) v.findViewById(R.id.buttonyes);
- yes.setOnClickListener(new OnClickListener(){
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- Toast.makeText(AndroidDialogActivity.this, "Hello World", Toast.LENGTH_SHORT).show();
- }});
- b.setView(v);
- return b.create();
b.setIcon(R.drawable.ic_launcher);b.setTitle("自定义对话框");LayoutInflater li=LayoutInflater.from(this);View v=li.inflate(R.layout.info, null);Button yes=(Button) v.findViewById(R.id.buttonyes);yes.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v) {// TODO Auto-generated method stubToast.makeText(AndroidDialogActivity.this, "Hello World", Toast.LENGTH_SHORT).show();}});b.setView(v);return b.create();
效果图
下载工程:下载
转自:http://blog.csdn.net/zhy_cheng/article/details/8003467
转自:http://www.cnblogs.com/Gaojiecai/archive/2011/12/10/2283156.html
- Andriod 对话框
- Andriod 对话框
- andriod 半透明对话框
- Andriod 常用对话框
- andriod之对话框--标准对话框、列表对话框、自定义对话框
- Andriod自定义对话框,列表对话框,标准对话框使用案例
- 哪些年遇到过的Andriod问题(4)对话框 dialog
- Andriod 的 弹出的对话框时的动画DEMO
- 安卓Andriod使用入门(二十)【颜色对话框】
- andriod对话框,弹出框dialog以及点击item的响应
- andriod
- andriod
- Andriod
- andriod
- andriod
- Andriod
- Andriod
- Andriod
- shell if else
- JdbcTemplate与事务
- 情景模式
- SqlServer附加数据库问题集锦(一)
- redis中 hash类型的操作命令
- Andriod 对话框
- 关于把数据保存到oracle中的blob字段
- javascript根据鼠标和键盘复制页面内容验证是否登录
- 新建ArcGIS Project for Android项目失败解决方案
- 静态成员变量和静态成员函数(static)
- Perl调试
- How to do Photoshop-like effects in SVG
- [举重若轻]python+django+mysql web开发入门学习之orm
- Apache无法启动报 the requested operationhas failed 错误