单击"更多"按钮,显示出多选框

运行截图:

程序结构

(本想通过Button中android:background使用drawable资源下的图片作为按钮背景,设计太丑就去掉了Σ(= = !))

package com.example.asus.a7gary01;

import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button; public class MainActivity extends AppCompatActivity { private Button button; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.更多); button.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setItems(getResources().getStringArray(R.array.Gary), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) { }
});
builder.show();
}
});
}
}

MainActivity

<?xml version="1.0" encoding="utf-8"?>
<TableLayout 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.asus.a7gary01.MainActivity"> <TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"> <Button
android:id="@+id/添加"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="添加" /> <Button
android:id="@+id/保存"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="保存" /> <Button
android:id="@+id/发送"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="发送" />
</TableRow> <TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"> <Button
android:id="@+id/详细"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="详细" /> <Button
android:id="@+id/查找"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="查找" /> <Button
android:id="@+id/更多"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="更多" />
</TableRow> </TableLayout>

activity_main.xml

<resources>
<string name="app_name">7Gary01</string>
<array name="Gary">
<item>帮助</item>
<item>分享</item>
<item>删除</item>
<item>拨号</item>
</array>
</resources>

strings.xml

一、界面布局

使用表格布局,放置六个按钮

二、功能实现

添加对Button的点击事件响应

 button.setOnClickListener(new OnClickListener() {

            @Override
public void onClick(View v) {
// 创建构建器
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); //在点击侦听器上建立新的对话框接口
//getResources()获得资源
builder.setItems(getResources().getStringArray(R.array.Gary), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) { }
});
//显示多选框
builder.show();
}
});

AlertDialog的六种创建方式:传送门

创建AlertDialog的步骤:

  1、创建AlertDialog.Builder对象

  2、调用Builder对象的setTitle方法设置标题,setIcon方法设置图标

  3、调用Builder相关方法如setMessage方法、setItems方法、setSingleChoiceItems方法、setMultiChoiceItems方法、setAdapter方法、setView方法设置不同类型的对话框内容。

  4、调用setPositiveButton、setNegativeButton、setNeutralButton设置多个按钮

  5、调用Builder对象的create()方法创建AlertDialog对象

  6、调用AlertDialog对象的show()方法将对话框显示出来

Android_(控件)使用AlertDialog实现点击Button显示出多选框的更多相关文章

  1. Qt树形控件QTreeView使用1——节点的添加删除操作 复选框的设置

    QtreeView是ui中最常用的控件,Qt中QTreeWidget比QTreeView更简单,但没有QTreeView那么灵活(QTreeWidget封装的和MFC的CTreeCtrl很类似,没有m ...

  2. VS编程,编辑WPF过程中,点击设计器中界面某一控件,在XAML中高亮突出显示相应的控件代码的设置方法。

    原文:VS编程,编辑WPF过程中,点击设计器中界面某一控件,在XAML中高亮突出显示相应的控件代码的设置方法. 版权声明:我不生产代码,我只是代码的搬运工. https://blog.csdn.net ...

  3. WPF解决按钮上被透明控件遮盖时无法点击问题

    原文:WPF解决按钮上被透明控件遮盖时无法点击问题 IsHitTestVisible="False" 在控件上设置如上属性即可,即可让透明控件不触发点击效果

  4. 背水一战 Windows 10 (31) - 控件(按钮类): ButtonBase, Button, HyperlinkButton, RepeatButton, ToggleButton, AppBarButton, AppBarToggleButton

    [源码下载] 背水一战 Windows 10 (31) - 控件(按钮类): ButtonBase, Button, HyperlinkButton, RepeatButton, ToggleButt ...

  5. 控件(按钮类): ButtonBase, Button, HyperlinkButton, RepeatButton, ToggleButton, AppBarButton, AppBarToggleButton

    介绍背水一战 Windows 10 之 控件(按钮类) ButtonBase Button HyperlinkButton RepeatButton ToggleButton AppBarButton ...

  6. [控件]unigui移动端下Unidatepicker时间显示解决方案

    [控件]unigui移动端下Unidatepicker时间显示解决方案 http://tz10000.com/kong-jian-unigui-yi-dong-duan-xia-unidatepick ...

  7. WPF 控件被禁用,悬浮提示不显示问题

    原文:WPF 控件被禁用,悬浮提示不显示问题 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/BYH371256/article/details/89 ...

  8. easyui combobox下拉框复制后再禁用,点击不会出现下拉框

    easyui combobox下拉框禁用,点击不会出现下拉框 需要做到,在给easyui combobox赋值后,再禁用easyui combobox 解决办法: $("#time-sele ...

  9. js进阶 12-15 jquery如何实现点击button显示列表,点击其它位置隐藏列表

    js进阶 12-15 jquery如何实现点击button显示列表,点击其它位置隐藏列表 一.总结 一句话总结:在button中阻止事件冒泡. 1.如何咋button中阻止事件冒泡(两种方法)? ev ...

随机推荐

  1. 一次简单的springboot+dubbo+flume+kafka+storm+redis系统

    最近无事学习一下,用springboot+dubbo+flume+kafka+storm+redis做了一个简单的scenic系统 scenicweb:展现层,springboot+dubbo sce ...

  2. HashMap—— values() remove方法 containsKey()方法 containsValue()方法

    values()方法:看下面的实例,就是把所有的value值封装成一个connection型的数组 Map<Integer,Student> students=new HashMap< ...

  3. Access to XMLHttpRequest at 'http://localhost:8090/user/getotp' from origin 'null' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

    ajax跨域请求错误 解决: springboot中: 加注解 @CrossOrigin

  4. git统计某个时间段写的代码行数

    1. 任务需要 领导想每个迭代统计一下,当前迭代开发的代码数量是多少 2. 解决方法 git log --stat --since=2019-09-12 --until=2019-09-27 | pe ...

  5. python常用模块:logging、hashlib、re

    今日内容主要有:一.logging模块二.logging模块的使用三.hashlib模块四.re模块 一.logging模块 import logging # 1 日志的级别 logging.debu ...

  6. CentOS7连接无线网络

    背景  CentOS7.6最小化安装,没有网线,幸好有无线网卡.下面我们直接进入主题.  附:安装教程 主题--连接无线网络 最小化安装后,裸机没有ifconfig,没有iw,最可怕的是没有网线 但我 ...

  7. CentOS7.x卸载与安装MySQL5.7的操作过程以及编码格式的修改

    一.MySQL5.7的卸载 1.1yum方式查看yum是否安装过mysql cd yum list installed mysql* 如或显示了列表,说明系统中有MySQL 如上显示,我已经安装了my ...

  8. 内核模式构造-Mutex构造(RecursiveAutoResetEvent)

    internal sealed class RecursiveAutoResetEvent : IDisposable { private AutoResetEvent m_lock = new Au ...

  9. 认识并初步应用GitHub——C++

    好好学习,天天向上 一.这是一个根据规定的开头 GIT的地址 https://github.com/Notexcellent GIT的用户名 Notexcxllent 学号后五位 82405 博客地址 ...

  10. Hdu 1564 1*2矩阵覆盖棋局博弈

    n*n的矩阵如果n为偶数 则可被1*2的矩形完全覆盖 如果n为奇数 则除起点外 其他各自可被1*2矩形完全覆盖 所以当n为偶数 先手赢 n为奇数 后手赢 #include<bits/stdc++ ...