服务器之家

服务器之家 > 正文

浅析Android中强大的Dialog

时间:2021-02-06 13:27     来源/作者:Android开发网

android中经常会使用到dialog(弹出框效果),而且功能非常强大,可以模拟出n种弹出框效果。如图所示:

浅析Android中强大的Dialog

下面将通过一个小实例,来像大家展示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>
标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
返回顶部