浅析Android中强大的Dialog

 更新时间:2013年10月30日 10:08:30   作者:  
下面将通过一个小实例,来像大家展示Android中功能强大的Dialog。代码都写了详细的注释,读者不妨试着手动去敲

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
    {

  @Override
  public void onClick(View v) {
   // TODO Auto-generated method stub
   Button button =(Button) v;//接收被点击的Button对象
   AlertDialog.Builder builder =new AlertDialog.Builder(MainActivity.this);//定义一个弹出框对象
   builder.setIcon(R.drawable.aa);//设置弹出框的ICON

   switch (v.getId())
   {
   case R.id.btnConfim :
    builder.setTitle("确定提交吗?");//设置弹出框的标题
    builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { 
     @Override
     public void onClick(DialogInterface dialog, int which) {
      showDialogMSG("您选择了确定");
     }
    });
    builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

     @Override
     public void onClick(DialogInterface dialog, int which) {
      showDialogMSG("您选择了取消");  
     }
    });

    break;
   case R.id.btnMore:
    builder.setTitle("评价");//标题
    builder.setMessage("说说李孝利在你心里的印象");//弹出框消息
    builder.setPositiveButton("性感",new DialogInterface.OnClickListener() {

     @Override
     public void onClick(DialogInterface dialog, int which) {
      // TODO Auto-generated method stub
         showDialogMSG("您觉得她很性感"); 
     }
    });
    builder.setNeutralButton("漂亮", new DialogInterface.OnClickListener() {

     @Override
     public void onClick(DialogInterface dialog, int which) {
      // TODO Auto-generated method stub
       showDialogMSG("您觉得她很漂亮"); 
     }
    });
    builder.setNegativeButton("风骚", new DialogInterface.OnClickListener() {

     @Override
     public void onClick(DialogInterface dialog, int which) {
      // TODO Auto-generated method stub
      showDialogMSG("您觉得她很风骚");
     }
    });
    break;
case R.id.BtnList :
    builder.setTitle("列表框");
    builder.setItems(items, new DialogInterface.OnClickListener() {

     @Override
     public void onClick(DialogInterface dialog, int which) {
      // TODO Auto-generated method stub
     showDialogMSG("您选择了ID为"+which+"的人,名字叫:"+items[which]); 
     }
    });
    break;
   case R.id.btnRidio:
     mSingleChoiceID = -1;
    builder.setTitle("单选框");
    builder.setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() {

     @Override
     public void onClick(DialogInterface dialog, int which) {
      // TODO Auto-generated method stub
      mSingleChoiceID=which;
      showDialogMSG("您选择了ID是"+mSingleChoiceID+",名字为:"+items[which]);
     }
    });
    builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

     @Override
     public void onClick(DialogInterface dialog, int which) {
      // TODO Auto-generated method stub
      if(mSingleChoiceID>-1)
      {
       showDialogMSG("您最终选择的是"+items[mSingleChoiceID]);
      }else
      {
       showDialogMSG("您最终选择的是"+items[0]);
      }
     }
    });
    builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

     @Override
     public 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() {

     @Override
     public void onClick(DialogInterface dialog, int which, boolean isChecked) {
      // TODO Auto-generated method stub
      if(isChecked)
      {
       MultiChoiceID.add(which);
       showDialogMSG("您选择了"+items[which]);
      }else
      {
       MultiChoiceID.remove(which);
      }

     }
    });
    builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

     @Override
     public void onClick(DialogInterface dialog, int which) {
      // TODO Auto-generated method stub

     String 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() {

     @Override
     public 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>

相关文章

最新评论