浅析Android中强大的Dialog

前端技术 2023/09/02 Android

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();//创建并显示弹出框 

   
  }

   
    }

   
}

本文地址:https://www.stayed.cn/item/4050

转载请注明出处。

本站部分内容来源于网络,如侵犯到您的权益,请 联系我

我的博客

人生若只如初见,何事秋风悲画扇。