Android之自定义AlertDialog的实现方法(一)
(一)Dialog简介
Dialog是Android开发中需要经常使用的系统组件之一,AlertDialog可以显示一个、两个等多个按钮,使用setMessage()方法可以只显示字符串提示信息,当然用户也可以自定义自己的AlertDialog。
构造方法
更改主题
2. 常用方法
(二)代码实现
MainActivity代码
package com.example.mydialog;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.widget.ListAdapter;
import android.widget.SimpleAdapter;
public class MainActivity extends Activity {
private ArrayList<HashMap<String, Object>> listItem;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
openDialog();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void openDialog() {
new AlertDialog.Builder(MainActivity.this).setTitle("自定义Dialog")
.setIcon(R.drawable.albums)
.setAdapter(getAdaper(), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
// 获取选中项的内容
Log.d("测试", listItem.get(which).get("ItemManager")
.toString());
}
}).show();
}
public ListAdapter getAdaper() {
listItem = new ArrayList<HashMap<String, Object>>();
HashMap<String, Object> map1 = new HashMap<String, Object>();
map1.put("ImageManager", R.drawable.compose);
map1.put("ItemManager", "录入");
listItem.add(map1);
HashMap<String, Object> map2 = new HashMap<String, Object>();
map2.put("ImageManager", R.drawable.camera);
map2.put("ItemManager", "拍照");
listItem.add(map2);
HashMap<String, Object> map4 = new HashMap<String, Object>();
map4.put("ImageManager", R.drawable.delete1);
map4.put("ItemManager", "删除");
listItem.add(map4);
SimpleAdapter listItemAdapter = new SimpleAdapter(this, listItem,
R.layout.mydialog,
new String[] { "ImageManager", "ItemManager" }, new int[] {
R.id.p_w_picpath, R.id.text });
return listItemAdapter;
}
}mydialog.xml文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="horizontal"android:gravity="center_vertical"><ImageView android:id="@+id/p_w_picpath"android:layout_width="38dp"android:layout_height="38dp"/><TextViewandroid:id="@+id/text"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentRight="true"android:layout_centerVertical ="true"android:textColor="#000000" /></RelativeLayout>
效果如下:
转载于:https://blog.51cto.com/6687431/1421226
Android之自定义AlertDialog的实现方法(一)相关推荐
- android sqlite自定义函数,Android中自定义一个View的方法详解
本文实例讲述了Android中自定义一个View的方法.分享给大家供大家参考,具体如下: Android中自定义View的实现比较简单,无非就是继承父类,然后重载方法,即便如此,在实际编码中难免会遇到 ...
- android 自定义搜索框edittext,Android编程自定义搜索框实现方法【附demo源码下载】...
本文实例讲述了Android编程自定义搜索框实现方法.分享给大家供大家参考,具体如下: 先来看效果图吧~ 分析:这只是模拟了一个静态数据的删除与显示 用EditText+PopupWindow+lis ...
- Android中自定义view的onMeasure()方法详谈
背景 理解MeasureSpec MeasureSpec 情况分析 结合图例分析 总结 A little bit of progress every day!Come on! 背景 首先关于自定义vi ...
- Android之自定义AlertDialog无法监听控件
参考:http://www.cnblogs.com/511mr/archive/2011/10/21/2220253.html 要做一个自定义的弹出框,以前都是用一个Activity来实现,总觉得不是 ...
- 【eoe教程】Android中自定义视图的绘制方法
原文链接 :http://android.eoe.cn/topic/ui 自定义视图最重要的部分是它的外观.你可以根据应用的需求简单或复杂的实现它. 这个教程包含了最常见的操作. 重写onDraw() ...
- Android 自定义AlertDialog,调用方法与系统一致
2019独角兽企业重金招聘Python工程师标准>>> 由于android原生的AlertDialog都一致,有时为了和你的项目的Dialog保持一致,你最先想到的就是有没有Aler ...
- android activity传值到dialog,android 自定义AlertDialog 与Activity相互传递数据
**主要实现功能:** 1.从Activity的TextView中获取字符串设置到AlertDialog的TextView和EditText中 2.将AlertDialog的EditText中的值设置 ...
- Android 自定义AlertDialog类
还是先从最简单的开始吧,然后一步一步的扩展. 为了保证软件上所谓的低耦合度和可重用性,这里不得不需要单独建立一个类CustomerDialog,然后继承AlertDialog public class ...
- Android自定义AlertDialog的控件获取操作
Android自定义AlertDialog的控件获取操作 在自定义的AlertDialog布局虽然可以显示,但是试过很多方法都不能获得其中的控件进行操作,找了很多方法最后这种方法可以. dialog的 ...
最新文章
- Linux系统下wetty安装和使用说明
- Mycat连接数据库之后导致表名全小写的问题分析研究
- Java编程思想之-匿名内部类
- Hadoop:Hadoop简介及环境配置
- Hyperledger Fabric 网络搭建详解
- PHP+jQuery.photoClip.js支持手势的图片裁剪上传实例
- python 实现代码雨(转载)
- 射频识别技术——数据编码
- linux pam鉴定令牌错误,linux – chsh:PAM身份验证失败
- 访问项目,页面打开太慢解决方式
- android音乐播放器——通过webview下载歌曲
- 数据库考试内容(MYSQL)
- Kindle使用指南
- android 打印小票格式,安卓端小票机设置
- mysql 定时任务编写
- PHP 5.3 下载时 VC9、VC6、Thread Safe、Non Thread Safe
- 计算机网络中的搜索引擎是指根据一定的策略,搜索引擎的常用使用技巧
- 在疫情期间微分销商城如何运营
- format转换方法
- 天龙八部哪个服务器里面人数最多的,天龙八部怀旧服:不删档各大门派人数出来了!你猜哪个门派人多?...