android 自定义方法,Android之自定义AlertDialog的实现方法(一)

本文介绍了如何在Android中自定义AlertDialog,包括Dialog的简介、常用构造方法和更改主题的方法。通过示例代码展示了如何创建一个包含多个选项的对话框,并提供了点击监听。示例中利用HashMap存储数据并使用SimpleAdapter填充列表。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

(一)Dialog简介

Dialog是Android开发中需要经常使用的系统组件之一,AlertDialog可以显示一个、两个等多个按钮,使用setMessage()方法可以只显示字符串提示信息,当然用户也可以自定义自己的AlertDialog。构造方法

49e7975f793e070c546de72d5799816e.png

更改主题

15f73ca1dba2a5f4e84f2bd58310ec78.png

2.  常用方法

5acf9ff126a4b269e6b055074869c773.png

(二)代码实现

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> 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 map1 = new HashMap();

map1.put("ImageManager", R.drawable.compose);

map1.put("ItemManager", "录入");

listItem.add(map1);

HashMap map2 = new HashMap();

map2.put("ImageManager", R.drawable.camera);

map2.put("ItemManager", "拍照");

listItem.add(map2);

HashMap map4 = new HashMap();

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文件

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="horizontal"

android:gravity="center_vertical"

>

android:layout_width="38dp"

android:layout_height="38dp"

/>

android:id="@+id/text"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentRight="true"

android:layout_centerVertical ="true"

android:textColor="#000000" />

效果如下:

380fccafbd50f3535239df164e9b50ba.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值