Android Dialog 学习汇总

fmms 13年前

一、界面效果
运行界面

Android Dialog 学习汇总

---------------------------------------------------------------------------------------

部分效果

自定义登录对话框

Android Dialog 学习汇总

圆形(转圈)进度条

Android Dialog 学习汇总


长形进度条

Android Dialog 学习汇总


多选按钮对话框

Android Dialog 学习汇总


单选按钮对话框

Android Dialog 学习汇总


带多个按钮的提示对话框

Android Dialog 学习汇总


带确定取消按钮的提示对话框

Android Dialog 学习汇总

---------------------------------------------------------------------------------------------------------

二、知识点
1 AlertDialog.Builder属性
* setTitle: 为对话框设置标题 ;
* setIcon : 为对话框设置图标;
* setMessage: 为对话框设置内容;
* setView :  给对话框设置自定义样式 ;
* setItems: 设置对话框要显示的一个list,一般用于显示几个命令时;
* setMultiChoiceItems:用来设置对话框显示一系列的复选框;
* setNeutralButton : 响应中立行为的点击;
* setPositiveButton : 响应Yes/Ok的点击 ;
* setNegativeButton :响应No/Cancel的点击 ;
* create : 创建对话框 ;
* show : 显示对话框;
2 ProgressDialog属性
*setProgressStyle:      设置进度条风格,风格为圆形,旋转的;
*setTitlt:                          设置ProgressDialog 标题;
*setMessage:              设置ProgressDialog提示信息;
*setIcon:                         设置ProgressDialog标题图标;
*setIndeterminate:    设置ProgressDialog 的进度条是否不明确;
*setCancelable:         设置ProgressDialog 是否可以按返回键取消;
*setButton:              设置ProgressDialog 的一个Button(需要监听Button事件);
*show:                     显示ProgressDialog。

-----------------------------------------------------------------------------------------

三、源码
1 布局文件:dialog_demo.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="wrap_content"       android:orientation="vertical" >          <TextView   android:layout_width="fill_parent"           android:layout_height="wrap_content"           android:gravity="center_horizontal"           android:paddingBottom="10dp"           android:paddingTop="8dp"           android:text="SkySeraph Android学习专题:Dialog"           android:textColor="#FFFF00"           android:textSize="15dp" >       </TextView>          <LinearLayout   android:layout_width="fill_parent"           android:layout_height="wrap_content"           android:layout_gravity="center"           android:orientation="vertical" >              <Button   android:id="@+id/dialg_demo_btn01"               android:layout_width="fill_parent"               android:layout_height="wrap_content"               android:text="简单提示对话框"                android:textSize="12dp" />              <Button   android:id="@+id/dialg_demo_btn02"               android:layout_width="fill_parent"               android:layout_height="wrap_content"               android:text="带确定取消按钮的提示对话框"                android:textSize="12dp" />              <Button   android:id="@+id/dialg_demo_btn03"               android:layout_width="fill_parent"               android:layout_height="wrap_content"               android:text="带多个按钮的提示对话框"                android:textSize="12dp" />              <Button   android:id="@+id/dialg_demo_btn04"               android:layout_width="fill_parent"               android:layout_height="wrap_content"               android:text="单选按钮对话框"                android:textSize="12dp" />              <Button   android:id="@+id/dialg_demo_btn05"               android:layout_width="fill_parent"               android:layout_height="wrap_content"               android:text="多选按钮对话框"                android:textSize="12dp" />                      <Button   android:id="@+id/dialg_demo_btn06"               android:layout_width="fill_parent"               android:layout_height="wrap_content"               android:text="列表对话框"                android:textSize="12dp" />              <Button   android:id="@+id/dialg_demo_btn07"               android:layout_width="fill_parent"               android:layout_height="wrap_content"               android:text="自定义对话框"               android:textSize="12dp"/>                      <Button   android:id="@+id/dialg_demo_btn08"               android:layout_width="fill_parent"               android:layout_height="wrap_content"               android:text="长形进度条"               android:textSize="12dp"/>                      <Button   android:id="@+id/dialg_demo_btn09"               android:layout_width="fill_parent"               android:layout_height="wrap_content"               android:text="圆形(转圈)进度条"               android:textSize="12dp"/>              <Button   android:id="@+id/dialg_demo_btn10"               android:layout_width="fill_parent"               android:layout_height="wrap_content"               android:text="..."               android:textSize="12dp" />          </LinearLayout>      </LinearLayout>
2 java代码:dialog_demo.java
public class dialog_demo extends Activity   {       private static final int MAX_PROGRESS = 100;    //进度条最大数       private ProgressDialog mProgressDialog  = null; //进度条       final String[] m_Items = {"Frist","Second","Third"};       int mSingleChoiceID = -1;    //记录单选中的ID       ArrayList <Integer>MultiChoiceID = new ArrayList <Integer>();//记录多选选中的id号          // ////////////////////////////////////////////////////////////////////////////////////       @Override       protected void onCreate(Bundle savedInstanceState)       {           // TODO Auto-generated method stub           super.onCreate(savedInstanceState);           setContentView(R.layout.dialog_demo);           findViews();       }          // ////////////////////////////////////////////////////////////////////////////////////       private void findViews()       {                   // //////////////////////////////////////////////////////////////////////////////           /* 【简单提示对话框】 */           Button btn1 = (Button) findViewById(R.id.dialg_demo_btn01);           btn1.setOnClickListener(new OnClickListener()           {               public void onClick(View v)               {                   // TODO Auto-generated method stub                   new AlertDialog.Builder(dialog_demo.this).setTitle("简单提示对话框").setMessage("这是提示信息")                           .show();                   return;               }           });           // //////////////////////////////////////////////////////////////////////////////           /* 【带确定取消按钮的提示对话框】 */           Button btn2 = (Button) findViewById(R.id.dialg_demo_btn02);           btn2.setOnClickListener(new OnClickListener()           {               public void onClick(View v)               {                   // TODO Auto-generated method stub                   AlertDialog.Builder dialog02 = new AlertDialog.Builder(dialog_demo.this);                   dialog02.setTitle("带确定取消按钮的提示对话框");                   dialog02.setIcon(R.drawable.qq);                   dialog02.setMessage("这是提示内容");                   dialog02.setPositiveButton("确定", new DialogInterface.OnClickListener()                   {                       public void onClick(DialogInterface dialoginterface, int which)                       {                           Toast.makeText(dialog_demo.this, "你选择了确定", Toast.LENGTH_LONG).show();                       }                   });                   dialog02.setNegativeButton("取消", new DialogInterface.OnClickListener()                   {                       public void onClick(DialogInterface dialoginterface, int which)                       {                           Toast.makeText(dialog_demo.this, "你选择了取消", Toast.LENGTH_LONG).show();                       }                   });                   dialog02.create().show();                    return;               }           });           // //////////////////////////////////////////////////////////////////////////////           /* 【带多个按钮的提示对话框】 */           Button btn3 = (Button) findViewById(R.id.dialg_demo_btn03);           btn3.setOnClickListener(new OnClickListener()           {               public void onClick(View v)               {                   // TODO Auto-generated method stub                   AlertDialog.Builder dialog03 = new AlertDialog.Builder(dialog_demo.this);                   dialog03.setIcon(R.drawable.img1);                   dialog03.setTitle("带多个按钮的提示对话框");                   dialog03.setMessage("你最喜欢的球类运动是什么呢?");                   dialog03.setPositiveButton("篮球", new DialogInterface.OnClickListener()                   {                       public void onClick(DialogInterface dialoginterface, int which)                       {                           showDialog("篮球很不错");                       }                   });                   dialog03.setNeutralButton("乒乓球", new DialogInterface.OnClickListener()                   {                       public void onClick(DialogInterface dialoginterface, int which)                       {                           showDialog("乒乓球很不错");                       }                   });                   dialog03.setNegativeButton("足球", new DialogInterface.OnClickListener()                   {                       public void onClick(DialogInterface dialoginterface, int which)                       {                           showDialog("足球很不错");                       }                   });                   dialog03.create().show();                   return;               }           });           // //////////////////////////////////////////////////////////////////////////////           /*【单选按钮对话框】*/           Button btn4 = (Button) findViewById(R.id.dialg_demo_btn04);           btn4.setOnClickListener(new OnClickListener()           {               public void onClick(View v)               {                   // TODO Auto-generated method stub                   mSingleChoiceID = -1;                   AlertDialog.Builder dialog04 = new AlertDialog.Builder(dialog_demo.this);                   dialog04.setTitle("单选按妞");                   dialog04.setSingleChoiceItems(m_Items, 0, new DialogInterface.OnClickListener()                   {                       public void onClick(DialogInterface dialog, int whichButton)                       {                           mSingleChoiceID = whichButton;                           showDialog("你选择的id为" + whichButton + " , " + m_Items[whichButton]);                       }                   });                   dialog04.setPositiveButton("确定", new DialogInterface.OnClickListener()                   {                       public void onClick(DialogInterface dialog, int whichButton)                       {                           if (mSingleChoiceID > 0)                           {                               showDialog("你选择的是" + mSingleChoiceID);                           }                       }                   });                   dialog04.setNegativeButton("取消", new DialogInterface.OnClickListener()                   {                       public void onClick(DialogInterface dialog, int whichButton)                       {                                                      }                   });                   dialog04.create().show();                   return;               }           });           // //////////////////////////////////////////////////////////////////////////////           /*【多选按钮对话框】*/           Button btn5 = (Button) findViewById(R.id.dialg_demo_btn05);           btn5.setOnClickListener(new OnClickListener()           {               public void onClick(View v)               {                   // TODO Auto-generated method stub                   MultiChoiceID.clear();                   AlertDialog.Builder dialog05 = new AlertDialog.Builder(dialog_demo.this);                   dialog05.setTitle("多选按钮");                   dialog05.setMultiChoiceItems(m_Items, new boolean[]                   { false, false, false},                           new DialogInterface.OnMultiChoiceClickListener()                           {                               public void onClick(DialogInterface dialog, int whichButton,                                       boolean isChecked)                               {                                   if (isChecked)                                   {                                       MultiChoiceID.add(whichButton);                                       showDialog("你选择的id为" + whichButton + " , "                                               + m_Items[whichButton]);                                   } else                                   {                                       MultiChoiceID.remove(whichButton);                                   }                                  }                           });                   dialog05.create().show();                   return;               }           });           // //////////////////////////////////////////////////////////////////////////////           /*【列表框对话框】*/           Button btn6 = (Button) findViewById(R.id.dialg_demo_btn06);           btn6.setOnClickListener(new OnClickListener()           {               public void onClick(View v)               {                   // TODO Auto-generated method stub                   AlertDialog.Builder dialog06 = new AlertDialog.Builder(dialog_demo.this);                   dialog06.setTitle("列表框");                   dialog06.setItems(m_Items, new DialogInterface.OnClickListener()                   {                       public void onClick(DialogInterface dialog, int which)                       {                           // 点击后弹出窗口选择了第几项                           showDialog("你选择的id为" + which + " , " + m_Items[which]);                       }                   });                   dialog06.create().show();                   return;               }           });                   // //////////////////////////////////////////////////////////////////////////////           /*【自定义登录对话框】*/           Button btn7 = (Button) findViewById(R.id.dialg_demo_btn07);           btn7.setOnClickListener(new OnClickListener()           {               public void onClick(View v)               {                   // TODO Auto-generated method stub                   LayoutInflater factory = LayoutInflater.from(dialog_demo.this);                                   final View view = factory.inflate(R.layout.dialog_demo_login, null);// 获得自定义对话框                                      AlertDialog.Builder dialog07 = new AlertDialog.Builder(dialog_demo.this);                   dialog07.setIcon(R.drawable.qq);                   dialog07.setTitle("自定义登录对话框");                   dialog07.setView(view);                   dialog07.setPositiveButton("确定", new DialogInterface.OnClickListener()                   {                       public void onClick(DialogInterface dialog, int whichButton)                       {                              EditText userName = (EditText) view                                   .findViewById(R.id.dialog_demo_loginETUserName);                           EditText password = (EditText) view                                   .findViewById(R.id.dialog_demo_loginETPassWord);                           showDialog("姓名 :" + userName.getText().toString() + "密码:"                                   + password.getText().toString());                       }                   });                   dialog07.setNegativeButton("取消", new DialogInterface.OnClickListener()                   {                       public void onClick(DialogInterface dialog, int whichButton)                       {                           //Toast.makeText(dialog_demo.this, "你选择了取消", Toast.LENGTH_LONG).show();                           showDialog("你选择了取消");                       }                   });                   dialog07.create().show();                   return;               }           });           // //////////////////////////////////////////////////////////////////////////////           Button btn8 = (Button) findViewById(R.id.dialg_demo_btn08);           btn8.setOnClickListener(new OnClickListener()           {               public void onClick(View v)               {                   // TODO Auto-generated method stub                   mProgressDialog = new ProgressDialog(dialog_demo.this);//创建ProgressDialog对象                   mProgressDialog.setIcon(R.drawable.qq);// 设置ProgressDialog标题 图标                   mProgressDialog.setTitle("进度条窗口");// 设置ProgressDialog标题                    mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);//设置进度条风格,风格为长形                   mProgressDialog.setMax(MAX_PROGRESS);// 设置ProgressDialo进度条进度                    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()                   {                       @Override                       public void run()                        {                           int Progress = 0;                           while (Progress < MAX_PROGRESS)                           {                               try                               {                                                   mProgressDialog.setProgress(Progress++);                                     //mProgressDialog.incrementProgressBy(1);                                   Thread.sleep(100);                               } catch (Exception e)                               {                                   // TODO Auto-generated catch block                                   mProgressDialog.cancel();                                   //e.printStackTrace();                               }                           }                           };                   }.start();                   return;               }           });           // //////////////////////////////////////////////////////////////////////////////           /*【圆形(转圈)进度条】*/           Button btn9 = (Button) findViewById(R.id.dialg_demo_btn09);           btn9.setOnClickListener(new OnClickListener()           {               public void onClick(View v)               {                   // TODO Auto-generated method stub                   mProgressDialog = new ProgressDialog(dialog_demo.this);//创建ProgressDialog对象                   mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); //设置进度条风格,风格为圆形,旋转的                    mProgressDialog.setTitle("读取ing...");// 设置ProgressDialog标题                    mProgressDialog.setMessage("正在读取中请稍候...");// 设置ProgressDialog提示信息                    mProgressDialog.setIndeterminate(true);//设置ProgressDialog 的进度条不明确                   mProgressDialog.setCancelable(true);// 设置ProgressDialog 可以按退回键取消                   mProgressDialog.setButton("确定", new DialogInterface.OnClickListener()                   {                       public void onClick(DialogInterface dialog, int whichButton)                       {                           // 这里添加点击后的逻辑                       }                   });                   mProgressDialog.show();// 让ProgressDialog显示                   return;               }           });           // //////////////////////////////////////////////////////////////////////////////           /*【带补充对话框】*/           Button btn10 = (Button) findViewById(R.id.dialg_demo_btn10);           btn10.setOnClickListener(new OnClickListener()           {               public void onClick(View v)               {                   // TODO Auto-generated method stub                   return;               }           });           // //////////////////////////////////////////////////////////////////////////////       }          // ////////////////////////////////////////////////////////////////////////////////////       /*显示子函数*/       private void showDialog(String str)       {           new AlertDialog.Builder(dialog_demo.this).setMessage(str).show();           // Toast.makeText(dialog_demo.this, str, Toast.LENGTH_LONG).show();       }       // ////////////////////////////////////////////////////////////////////////////////////   }
3 自定义登录对话框:dialog_demo_login.xml
<?xml version="1.0" encoding="utf-8"?>   <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"       android:id="@+id/dialog_demo_login"       android:layout_width="fill_parent"       android:layout_height="fill_parent"       android:orientation="vertical" >          <TextView   android:id="@+id/dialog_demo_loginTVUserName"           android:layout_width="wrap_content"           android:layout_height="wrap_content"           android:text="姓名:"            android:textSize="18dp">       </TextView>          <EditText   android:id="@+id/dialog_demo_loginETUserName"           android:layout_width="wrap_content"           android:layout_height="wrap_content"           android:text=""           android:textSize="18dp" >       </EditText>          <TextView   android:id="@+id/dialog_demo_loginTVPassWord"           android:layout_width="wrap_content"           android:layout_height="wrap_content"           android:text="密码:"           android:textSize="18dp" >       </TextView>          <EditText   android:id="@+id/dialog_demo_loginETPassWord"           android:layout_width="wrap_content"           android:layout_height="wrap_content"           android:text=""           android:textSize="18dp">       </EditText>      </TableLayout>

四、Refs

【Android】对话框 AlertDialog :http://blog.csdn.net/feng88724/article/details/6171450

Android UI学习 - 对话框 (AlertDialog & ProgressDialog) http://android.blog.51cto.com/268543/333769 

Android软件开发之盘点所有Dialog对话框大合集(一) :http://blog.csdn.net/xys289187120/article/details/6601613

Android 对话框(Dialog)大全 建立你自己的对话框 :http://www.cnblogs.com/salam/archive/2010/11/15/1877512.html

转自:http://www.cnblogs.com/skyseraph/archive/2012/02/22/2362989.html