Android中强大的Dialog
来源:互联网 发布:2016年网络大电影分账 编辑:程序博客网 时间:2024/06/01 21:43
Android中经常会使用到Dialog(弹出框效果),而且功能非常强大,可以模拟出N种弹出框效果。如图所示:
下面将通过一个小实例,来像大家展示Android中功能强大的Dialog。代码都写了详细的注释,读者不妨试着手动去敲。
当然,由于时间的关系,还有个别功能没有完成,先有的几个展示效果中也可能有bug,代码也有很多需要优化的地方。望大家热心指出。
下面是代码(有点长哦):
package com.chaoyang.activity;import java.util.ArrayList;import android.app.Activity;import android.app.AlertDialog;import android.app.ProgressDialog;import android.content.DialogInterface;import android.os.Bundle;import android.view.View;import android.widget.Button;public class MainActivity extends Activity { /** Called when the activity is first created. */final String[] items={"张三","李四","王五","赵六","阿猫","阿狗"}; int mSingleChoiceID = -1; ArrayList <Integer>MultiChoiceID = new ArrayList <Integer>(); private ProgressDialog mProgressDialog = null; private static final int MAX_PROGRESS = 100; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); /*START 设置那些按钮的点击监听对象*/ ButtonOnclikListen listen =new ButtonOnclikListen(); Button btnConfim =(Button)this.findViewById(R.id.btnConfim); Button btnCheck =(Button)this.findViewById(R.id.btnCheck); Button btnCustom =(Button)this.findViewById(R.id.btnCustom); Button BtnList =(Button)this.findViewById(R.id.BtnList); Button btnMore =(Button)this.findViewById(R.id.btnMore); Button BtnProgressbar =(Button)this.findViewById(R.id.BtnProgressbar); Button btnRead =(Button)this.findViewById(R.id.btnRead); Button btnRidio =(Button)this.findViewById(R.id.btnRidio); btnConfim.setOnClickListener(listen); btnCheck.setOnClickListener(listen); btnConfim.setOnClickListener(listen); BtnList.setOnClickListener(listen); btnMore.setOnClickListener(listen); BtnProgressbar.setOnClickListener(listen); btnRead.setOnClickListener(listen); btnRidio.setOnClickListener(listen); /* END */ } //定义一个公共方法,弹出消息 private void showDialogMSG(String msg) { new AlertDialog.Builder(MainActivity.this) .setMessage(msg) .show(); } //定义一个内部类,继承至OnClickListener,专门来处理按钮点击事件(面向对象,灵活) private final class ButtonOnclikListen implements View.OnClickListener {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubButton button =(Button) v;//接收被点击的Button对象AlertDialog.Builder builder =new AlertDialog.Builder(MainActivity.this);//定义一个弹出框对象builder.setIcon(R.drawable.aa);//设置弹出框的ICONswitch (v.getId()){case R.id.btnConfim :builder.setTitle("确定提交吗?");//设置弹出框的标题builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {showDialogMSG("您选择了确定");}});builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {showDialogMSG("您选择了取消");}});break;case R.id.btnMore:builder.setTitle("评价");//标题builder.setMessage("说说李孝利在你心里的印象");//弹出框消息builder.setPositiveButton("性感",new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stub showDialogMSG("您觉得她很性感");}});builder.setNeutralButton("漂亮", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stub showDialogMSG("您觉得她很漂亮");}});builder.setNegativeButton("风骚", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stubshowDialogMSG("您觉得她很风骚");}});break;case R.id.BtnList :builder.setTitle("列表框");builder.setItems(items, new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stubshowDialogMSG("您选择了ID为"+which+"的人,名字叫:"+items[which]);}});break;case R.id.btnRidio: mSingleChoiceID = -1;builder.setTitle("单选框");builder.setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stubmSingleChoiceID=which;showDialogMSG("您选择了ID是"+mSingleChoiceID+",名字为:"+items[which]);}});builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stubif(mSingleChoiceID>-1){showDialogMSG("您最终选择的是"+items[mSingleChoiceID]);}else{showDialogMSG("您最终选择的是"+items[0]);}}});builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stub}});break;case R.id.btnCheck :MultiChoiceID.clear(); //先清空集合builder.setTitle("多项选择");builder.setMultiChoiceItems(items,new boolean[]{false,false,false,false,false,false}, new DialogInterface.OnMultiChoiceClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which, boolean isChecked) {// TODO Auto-generated method stubif(isChecked){MultiChoiceID.add(which);showDialogMSG("您选择了"+items[which]);}else{MultiChoiceID.remove(which);}}});builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stubString str="";for(int i = 0;i<MultiChoiceID.size();i++){str+=items[MultiChoiceID.get(i)]+",";}//去掉最后一个","if(str.length()>0){str = str.substring(0,str.length()-1);}showDialogMSG("你一共选择的有"+str);}});builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stub}});break;case R.id.BtnProgressbar:mProgressDialog =new ProgressDialog(MainActivity.this);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().start(); return;case R.id.btnCustom: break;case R.id.btnRead:break;}builder.create().show();//创建并显示弹出框} } }
布局文件部分,比较简单:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" ><TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <Button android:id="@+id/btnConfim" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="确定/取消" /> <Button android:id="@+id/btnMore" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="多个消息提示" /> <Button android:id="@+id/BtnList" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="列表框" /> <Button android:id="@+id/BtnProgressbar" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="进度条框" /> <Button android:id="@+id/btnRidio" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="单选框" /> <Button android:id="@+id/btnCheck" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="多选框" /> <Button android:id="@+id/btnCustom" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="自定义布局" /> <Button android:id="@+id/btnRead" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="读取进度框" /></LinearLayout>
- Android中强大的Dialog
- android activity以dialog形式展现,强大的原生!
- android中dialog(自定义dialog)的使用方法!!!
- Android中强大的SpannableStringBuilder
- android中Dialog的使用方法
- Android中Dialog的使用
- Android中Dialog的使用
- android中dialog的样式
- Android中Dialog的使用
- Android中Dialog的理解
- Android中Dialog的使用
- Android中Dialog的使用
- Android中Dialog的使用
- android中dialog的使用
- Android中Dialog的使用
- Android中Dialog的使用
- Android 中dialog的使用
- Android中Dialog的使用
- vim Can't open file for writing
- 在postgre中创建newguid()方法
- batch文件常用命令
- response中文乱码分析
- 开始学习
- Android中强大的Dialog
- C# Winform 应用的内存调优
- Android 用APN来获取手机号
- mesa opengl 安装
- Android 获取设备信息
- ubuntu用apt-get方法安装mysql5 + Apache2 + PHP5+Phpmyadmin
- Postfix+Dovecot邮件服务器配置指南,Postfix使用Dovecot认证,配置环境CentOS5.5
- Zendframework的注册模式
- taskset榨干服务器CPU:让进程运行在指定的CPU内核