android 单选、多选弹出菜单
菜单单选窗口:
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends Activity {
private String[] areas = new String[]{"全部","玉兰香苑", "张江地铁站", "金科路", "张江路", "紫薇路", "香楠小区" };
private boolean[] areaState=new boolean[]{true, false, false, false, false, false,false };
private RadioOnClick radioOnClick = new RadioOnClick(1);
private ListView areaCheckListView;
private ListView areaRadioListView;
private Button alertButton;
private Button checkBoxButton;
private Button radioButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
alertButton=(Button)findViewById(R.id.alertButton);
checkBoxButton=(Button)findViewById(R.id.checkBoxButton);
radioButton=(Button)findViewById(R.id.radioButton);
alertButton.setOnClickListener(new AlertClickListener());
checkBoxButton.setOnClickListener(new CheckBoxClickListener());
radioButton.setOnClickListener(new RadioClickListener());
}
/**
* 菜单弹出窗口
* @author xmz
*
*/
class AlertClickListener implements OnClickListener{
@Override
public void onClick(View v) {
new AlertDialog.Builder(MainActivity.this).setTitle("选择区域").setItems(areas,new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which){
Toast.makeText(MainActivity.this, "您已经选择了: " + which + ":" + areas[which],Toast.LENGTH_LONG).show();
dialog.dismiss();
}
}).show();
}
}
/**
* 多选框弹出菜单窗口
* @author xmz
*
*/
class CheckBoxClickListener implements OnClickListener{
@Override
public void onClick(View v) {
AlertDialog ad = new AlertDialog.Builder(MainActivity.this)
.setTitle("选择区域")
.setMultiChoiceItems(areas,areaState,new DialogInterface.OnMultiChoiceClickListener(){
public void onClick(DialogInterface dialog,int whichButton, boolean isChecked){
//点击某个区域
}
}).setPositiveButton("确定",new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,int whichButton){
String s = "您选择了:";
for (int i = 0; i < areas.length; i++){
if (areaCheckListView.getCheckedItemPositions().get(i)){
s += i + ":"+ areaCheckListView.getAdapter().getItem(i)+ " ";
}else{
areaCheckListView.getCheckedItemPositions().get(i,false);
}
}
if (areaCheckListView.getCheckedItemPositions().size() > 0){
Toast.makeText(MainActivity.this, s, Toast.LENGTH_LONG).show();
}else{
//没有选择
}
dialog.dismiss();
}
}).setNegativeButton("取消", null).create();
areaCheckListView = ad.getListView();
ad.show();
}
}
/**
* 单选弹出菜单窗口
* @author xmz
*
*/
class RadioClickListener implements OnClickListener {
@Override
public void onClick(View v) {
AlertDialog ad =new AlertDialog.Builder(MainActivity.this).setTitle("选择区域")
.setSingleChoiceItems(areas,radioOnClick.getIndex(),radioOnClick).create();
areaRadioListView=ad.getListView();
ad.show();
}
}
/**
* 点击单选框事件
* @author xmz
*
*/
class RadioOnClick implements DialogInterface.OnClickListener{
private int index;
public RadioOnClick(int index){
this.index = index;
}
public void setIndex(int index){
this.index=index;
}
public int getIndex(){
return index;
}
public void onClick(DialogInterface dialog, int whichButton){
setIndex(whichButton);
Toast.makeText(MainActivity.this, "您已经选择了: " + index + ":" + areas[index], Toast.LENGTH_LONG).show();
dialog.dismiss();
}
}
}
<?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"
>
<Button
android:id="@+id/alertButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="菜单选择窗口"
/>
<Button
android:id="@+id/checkBoxButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="多选菜单选择窗口"
/>
<Button
android:id="@+id/radioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="单选菜单选择窗口-1"
/>
</LinearLayout>
android 单选、多选弹出菜单的更多相关文章
- Android 使用PopupWindow实现弹出菜单
在本文当中,我将会与大家分享一个封装了PopupWindow实现弹出菜单的类,并说明它的实现与使用. 因对界面的需求,android原生的弹出菜单已不能满足我们的需求,自定义菜单成了我们的唯一选择,在 ...
- 【Android】创建Popwindow弹出菜单的两种方式
方法一的Activity package com.app.test02; import android.app.Activity; import android.os.Bundle; import a ...
- 【Android】5.6 弹出菜单(PopUp Menus)
分类:C#.Android.VS2015: 创建日期:2016-02-07 一.简介 功能描述:用户单击按钮弹出菜单.当用户选择一个菜单项,会触发MenuItemClick事件并让弹出的菜单消失:如果 ...
- Android于popWindow写弹出菜单
1.什么是popWindow? popWindow这是对话的方式!文字解说android的方式来使用对话框,这就是所谓的popWindow. 2.popWindow特征 Android的对话框有两种: ...
- android 长按弹出菜单,复制,粘贴,全选
<!-- 定义基础布局LinearLayout --> <LinearLayout xmlns:android="http://schemas.android.com/ap ...
- Android ListView两种长按弹出菜单方式
转自:http://www.cnblogs.com/yejiurui/p/3247527.html package com.wyl.download_demo; import java.util.Ar ...
- 【转】 教你如何创建类似QQ的android弹出菜单
原文地址:http://www.apkbus.com/android-18034-1-1.html 大家可能看到android的自带的系统菜单比较难看,如图: 2011-12-4 23:13 上传 下 ...
- 【转】android创建Popwindow弹出菜单的两种方式
方法一的Activity package com.app.test02; import android.app.Activity; import android.os.Bundle; import a ...
- Android开发技巧——使用PopupWindow实现弹出菜单
在本文当中,我将会与大家分享一个封装了PopupWindow实现弹出菜单的类,并说明它的实现与使用. 因对界面的需求,android原生的弹出菜单已不能满足我们的需求,自定义菜单成了我们的唯一选择,在 ...
随机推荐
- 【vijos1266】搜集环盖
题意 百事任何饮料的瓶盖上都会有一个百事球星的名字. 假设有\(n\)个不同的球星名字,每个名字出现的概率相同,平均需要买几瓶饮料才能凑齐所有的名字呢? 分析 设凑齐\(i\)个球星的期望次数为\(f ...
- Python--关于set
慕课网<Pyrhon入门>学习笔记 1.set 特性 set 持有一系列元素,这一点和 list 很像,但是set的元素没有重复,而且是无序的,这点和 dict 的 key很像. 可以将s ...
- sql server多表数据批量更新
update wset w.TagCount=x.TagCountfrom (select ItemID,COUNT(*) as TagCount from r where IsValid=1 gro ...
- JAVA 多态和异常处理作业——动手动脑以及课后实验性问题
1. 阅读以下代码(CatchWho.java),写出程序运行结果: 1) 源代码 public class CatchWho { public static void main(String[] ...
- 20145236 《Java程序设计》 第十周学习总结
20145236 <Java程序设计> 第十周学习总结 Java网络编程 Java网络编程技术 Java语言是在网络环境下诞生的,所以Java语言虽然不能说是对于网络编程的支持最好的语言, ...
- MATLAB实现矩阵分块相乘
要实现一下功能,这里$\bf{x}_i$为行向量 $${\bf{A}} = \left[ \begin{array}{l}{{\bf{x}}_1}\\{{\bf{x}}_2}\end{array} \ ...
- offsetLeft,Left,clientLeft的区别
offsetLeft,Left,clientLeft的区别 假设 obj 为某个 HTML 控件. obj.offsetTop 指 obj 相对于版面或由 offsetParent 属性指定的父坐标的 ...
- IE9中Media queries在iframe无效的解决方法
在css中有5个media querie @media screen and(min-width:0px)and(max-width:319px){ body {background-color:re ...
- win7_oracle11g_64位连接32位PLSQL_Developer
工具/原料 已经装好的64位Oracle数据库 window7_64位的操作系统 PLSQL_Developer 9.0以上版本(目前只有32位的):下面有下载连接! 官方的 instantcli ...
- [转]玩转Google开源C++单元测试框架Google Test系列
gtest的官方网站是: http://code.google.com/p/googletest/ 从官方的使用文档里,你几乎可以获得你想要的所有东西 http://code.google.com/p ...