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

Android实现弹出列表、单选、多选框

程序员文章站 2023-09-04 15:14:05
本文实例为大家分享了android实现弹出列表、单选、多选框的具体代码,供大家参考,具体内容如下 效果图如下: 需要建一个menu xml布局如下: &...

本文实例为大家分享了android实现弹出列表、单选、多选框的具体代码,供大家参考,具体内容如下

效果图如下:

Android实现弹出列表、单选、多选框Android实现弹出列表、单选、多选框Android实现弹出列表、单选、多选框Android实现弹出列表、单选、多选框

需要建一个menu

xml布局如下:

<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:context="com.example.lyp1020k.lv.mainactivity"
 android:orientation="vertical">
 
 <button
 android:id="@+id/button1"
 android:text="列表框"
 android:onclick="showlist"
 android:layout_width="match_parent"
 android:layout_height="wrap_content" />
 
 <button
 android:id="@+id/button2"
 android:text="单选列表"
 android:onclick="showsinglealertdialog"
 android:layout_width="match_parent"
 android:layout_height="wrap_content" />
 
 <button
 android:id="@+id/button3"
 android:text="多选按钮"
 android:onclick="showmutilalertdialog"
 android:layout_width="match_parent"
 android:layout_height="wrap_content" />
 
</linearlayout>

java代码如下:

import android.content.dialoginterface;
import android.support.v7.app.alertdialog;
import android.support.v7.app.appcompatactivity;
import android.os.bundle;
import android.view.menu;
import android.view.view;
import android.widget.button;
import android.widget.toast;
 
public class mainactivity extends appcompatactivity {
 
 private alertdialog alertdialog1; //信息框
 private alertdialog alertdialog2; //单选框
 private alertdialog alertdialog3; //多选框
 
 private button button1;
 private button button2;
 private button button3;
 
 @override
 protected void oncreate(bundle savedinstancestate) {
 super.oncreate(savedinstancestate);
 setcontentview(r.layout.activity_main);
 init();
 }
 
 public void init(){
 button1 = (button) findviewbyid(r.id.button1);
 button2 = (button) findviewbyid(r.id.button2);
 button3 = (button) findviewbyid(r.id.button3);
 }
 
 @override
 public boolean oncreateoptionsmenu(menu menu) {
 getmenuinflater().inflate(r.menu.mian, menu);
 return true;
 }
 
 public void showlist(view view){
 final string[] items = {"列表1", "列表2", "列表3", "列表4"};
 alertdialog.builder alertbuilder = new alertdialog.builder(this);
 alertbuilder.settitle("这是列表框");
 alertbuilder.setitems(items, new dialoginterface.onclicklistener() {
  @override
  public void onclick(dialoginterface dialoginterface, int i) {
  toast.maketext(mainactivity.this, items[i], toast.length_short).show();
  alertdialog1.dismiss();
  }
 });
 alertdialog1 = alertbuilder.create();
 alertdialog1.show();
 }
 
 public void showsinglealertdialog(view view){
 final string[] items = {"单选1", "单选2", "单选3", "单选4"};
 alertdialog.builder alertbuilder = new alertdialog.builder(this);
 alertbuilder.settitle("这是单选框");
 alertbuilder.setsinglechoiceitems(items, 0, new dialoginterface.onclicklistener() {
  @override
  public void onclick(dialoginterface dialoginterface, int i) {
  toast.maketext(mainactivity.this, items[i], toast.length_short).show();
  }
 });
 
 alertbuilder.setpositivebutton("确定", new dialoginterface.onclicklistener() {
  @override
  public void onclick(dialoginterface dialoginterface, int i) {
  alertdialog2.dismiss();
  }
 });
 
 alertbuilder.setnegativebutton("取消", new dialoginterface.onclicklistener() {
  @override
  public void onclick(dialoginterface dialoginterface, int i) {
  alertdialog2.dismiss();
  }
 });
 
 alertdialog2 = alertbuilder.create();
 alertdialog2.show();
 }
 
 public void showmutilalertdialog(view view){
 final string[] items = {"多选1", "多选2", "多选3", "多选4"};
 alertdialog.builder alertbuilder = new alertdialog.builder(this);
 alertbuilder.settitle("这是多选框");
 /**
  *第一个参数:弹出框的消息集合,一般为字符串集合
  * 第二个参数:默认被选中的,布尔类数组
  * 第三个参数:勾选事件监听
  */
 alertbuilder.setmultichoiceitems(items, null, new dialoginterface.onmultichoiceclicklistener() {
  @override
  public void onclick(dialoginterface dialoginterface, int i, boolean ischecked) {
  if (ischecked){
   toast.maketext(mainactivity.this, "选择" + items[i], toast.length_short).show();
  }else {
   toast.maketext(mainactivity.this, "取消选择" + items[i], toast.length_short).show();
  }
  }
 });
 alertbuilder.setpositivebutton("确定", new dialoginterface.onclicklistener() {
  @override
  public void onclick(dialoginterface dialoginterface, int i) {
  alertdialog3.dismiss();
  }
 });
 
 alertbuilder.setnegativebutton("取消", new dialoginterface.onclicklistener() {
  @override
  public void onclick(dialoginterface dialoginterface, int i) {
  alertdialog3.dismiss();
  }
 });
 
 
 alertdialog3 = alertbuilder.create();
 alertdialog3.show();
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。