欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android使用AlertDialog实现对话框

程序员文章站 2023-12-10 17:31:58
示例: 一、确定对话框 alertdialog.builder builder = new alertdialog.builder(this); bui...

示例:

一、确定对话框

alertdialog.builder builder = new alertdialog.builder(this);
   builder.settitle("确认对话框");
   builder.seticon(r.drawable.icon_72);
   builder.setmessage("这里是对话框内容");
   builder.setpositivebutton("确定", new dialoginterface.onclicklistener() {
    @override
    public void onclick(dialoginterface arg0, int arg1) {
     // todo auto-generated method stub
     toast.maketext(alertdialog_text.this, "点击了确定按钮", 1).show();
    }
   });
   alertdialog dialog = builder.create();
   dialog.show(); //显示、

Android使用AlertDialog实现对话框

二、普通列表

final string[] items = new string[]{"语文","数学","英语","物理","化学"}; //列表项
   builder alertdialog = new alertdialog.builder(this); 
   alertdialog.settitle("你喜欢的课程").setitems(items, new dialoginterface.onclicklistener() {
    @override
    public void onclick(dialoginterface dialog, int which) {
     // todo auto-generated method stub
     toast.maketext(alertdialog_lianxi.this, items[which], toast.length_short).show();
    }
   });
   alertdialog.create().show();     //创建显示列表

Android使用AlertDialog实现对话框

三、单选列表

final string[] items_fruit = new string[]{"苹果","香蕉","橘子","西瓜","梨"};
   builder alerdialog = new alertdialog.builder(this);
   //设置列表标题
   alerdialog.settitle("你喜欢的水果");
   //设置单选列表
   alerdialog.setsinglechoiceitems(items_fruit, 0, new dialoginterface.onclicklistener() {
    @override
    public void onclick(dialoginterface dialog, int which) {
     // todo auto-generated method stub
     toast.maketext(alertdialog_lianxi.this, items_fruit[which], toast.length_short).show();
    }
   });
   //设置取消按钮并且设置响应事件
   alerdialog.setnegativebutton("取消", new dialoginterface.onclicklistener() {
    @override
    public void onclick(dialoginterface dialog, int which) {
     // todo auto-generated method stub
     //取消按钮响应事件
    }
   });
   //添加确定按钮 并且设置响应事件
   alerdialog.setpositivebutton("确定", new dialoginterface.onclicklistener() {  
    @override
    public void onclick(dialoginterface dialog, int which) {
     // todo auto-generated method stub
     //确定按钮响应事件
    }
   });
   alerdialog.create().show();//创建显示列表

Android使用AlertDialog实现对话框

四、多选列表

final string[] items_fruit1 = new string[]{"苹果","香蕉","橘子","西瓜","梨"}; //设置项
   final boolean[] items_fruit_selected = new boolean[]{true,false,false,false,false}; 
   builder alerdialog1 = new alertdialog.builder(this);
   //设置列表标题
   alerdialog1.settitle("你喜欢的水果");
   //设置多选列表
   alerdialog1.setmultichoiceitems(items_fruit1, items_fruit_selected, new dialoginterface.onmultichoiceclicklistener() {
    @override
    public void onclick(dialoginterface dialog, int which, boolean ischecked) {
     // todo auto-generated method stub
     items_fruit_selected[which] = ischecked;
    }
   });
   //设置取消按钮并且设置响应事件
   alerdialog1.setnegativebutton("取消", new dialoginterface.onclicklistener() {
    @override
    public void onclick(dialoginterface dialog, int which) {
     // todo auto-generated method stub
     //取消按钮响应事件
    }
   });
   //添加确定按钮 并且设置响应事件,将选择的项显示
   alerdialog1.setpositivebutton("确定", new dialoginterface.onclicklistener() {  
    @override
    public void onclick(dialoginterface dialog, int which) {
     // todo auto-generated method stub
     //确定按钮响应事件
     stringbuilder stringbuilder = new stringbuilder();
     for(int i=0;i<items_fruit_selected.length;i++)
     {
      if(items_fruit_selected[i] == true)
      {
       stringbuilder.append(items_fruit1[i]+"、");
      }
     }
     toast.maketext(alertdialog_lianxi.this, stringbuilder.tostring(), toast.length_short).show();
    }
   });
   alerdialog1.create().show();//创建显示列表

Android使用AlertDialog实现对话框

Android使用AlertDialog实现对话框

5、自定义布局对话框

Android使用AlertDialog实现对话框

对话框布局文件

<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:gravity="center_horizontal"
 android:orientation="vertical" >
 <edittext 
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:editable="false"
  android:text="自定义对话框"/>
 <textview 
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="这里是自定义对话框的内容"
  android:textsize="20dp"
  />
 <button 
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="确定"
  />
</linearlayout>

layout_dialog
package com.example.demo1;
import android.app.activity;
import android.app.alertdialog;
import android.content.dialoginterface;
import android.os.bundle;
import android.view.layoutinflater;
import android.view.view;
import android.view.view.onclicklistener;
import android.widget.button;
public class mainactivity extends activity {
 private button btn_opendialog;
 private view view;
 @override
 protected void oncreate(bundle savedinstancestate) {
  super.oncreate(savedinstancestate);
  setcontentview(r.layout.activity_main);
  btn_opendialog = (button) findviewbyid(r.id.id_submit);
  btn_opendialog.setonclicklistener(new onclicklistener() {
   @override
   public void onclick(view v) {
    // todo auto-generated method stub
    view = layoutinflater.from(mainactivity.this).inflate(r.layout.layout_dialog, null);
    alertdialog dialog = new alertdialog.builder(mainactivity.this)
          .settitle("主题")
          .seticon(r.drawable.ic_launcher)
          .setview(view)
          .setpositivebutton("取消", new dialoginterface.onclicklistener() {
          @override
          public void onclick(dialoginterface dialog, int which) {
           // todo auto-generated method stub
          }
         })
          .create();
    dialog.show();
   }
  });
 }
}

mainactivity.class

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持!