常见的一种方法:

[html] view plaincopyprint?

  1. AlertDialog.Builder builder;

  2. AlertDialog alertDialog;

  3. LayoutInflater inflater = getLayoutInflater();

  4. // 添加自定义的布局文件

  5. View layout = LayoutInflater.from(TestOne.this).inflate(

  6. R.layout.dialog, null);

  7. final TextView text = (TextView) layout.findViewById(R.id.tv1);

  8. // 添加点击事件

  9. text.setOnClickListener(new OnClickListener() {

  10. @Override

  11. public void onClick(View v) {

  12. // TODO Auto-generated method stub

  13. text.setText("call");

  14. }

  15. });

  16. builder = new AlertDialog.Builder(TestOne.this);

  17. alertDialog = builder.create();

  18. // 去掉边框的黑色,因为设置的与四周的间距为0

  19. alertDialog.setView(layout, 0, 0, 0, 0);

  20. alertDialog.show();

  21. // 修改大小

  22. WindowManager.LayoutParams params = alertDialog.getWindow()

  23. .getAttributes();

  24. params.width = 350;

  25. params.height = 200;

  26. alertDialog.getWindow().setAttributes(params);

这样 ,重新给它填充自定义的布局视图,但缺乏可扩展性,而且每次使用还得重新定义。

重写AlertDialog类,定义方法:

[html] view plaincopyprint?

  1. /**

  2. * 自定义的对话框

  3. */

  4. public abstract class MyAlerDialog extends AlertDialog implements

  5. android.view.View.OnClickListener {

  6. protected MyAlerDialog(Context context) {

  7. super(context);

  8. // TODO Auto-generated constructor stub

  9. }

  10. /**

  11. * 布局中的其中一个组件

  12. */

  13. private TextView txt;

  14. @Override

  15. protected void onCreate(Bundle savedInstanceState) {

  16. // TODO Auto-generated method stub

  17. super.onCreate(savedInstanceState);

  18. // 加载自定义布局

  19. setContentView(R.layout.dialog);

  20. // setDialogSize(300, 200);

  21. txt = (TextView) findViewById(R.id.tv1);

  22. txt.setOnClickListener(this);

  23. }

  24. /**

  25. * 修改 框体大小

  26. *

  27. * @param width

  28. * @param height

  29. */

  30. public void setDialogSize(int width, int height) {

  31. WindowManager.LayoutParams params = getWindow().getAttributes();

  32. params.width = 350;

  33. params.height = 200;

  34. this.getWindow().setAttributes(params);

  35. }

  36. public abstract void clickCallBack();

  37. /**

  38. * 点击事件

  39. */

  40. @Override

  41. public void onClick(View v) {

  42. // TODO Auto-generated method stub

  43. if (v == txt) {

  44. clickCallBack();

  45. }

  46. }

  47. }

在活动中使用:

[html] view plaincopyprint?

  1. MyAlerDialog mydialog = new MyAlerDialog(this) {

  2. // 重写callback方法

  3. @Override

  4. public void clickCallBack() {

  5. // TODO Auto-generated method stub

  6. btn.setText("call");

  7. }

  8. };

  9. mydialog.show();

自己写的功能就封装了两个,有需要的童鞋可以很容易的扩展。这种方法,显然相对于上一种要有优势得多啦。

Android自定义AlertDialog的更多相关文章

  1. Android 自定义AlertDialog退出对话框

    Android 自定义AlertDialog退出对话框 转 https://blog.csdn.net/wkh11/article/details/53081634在项目中很多时候会出现点击返回键出现 ...

  2. Android 自定义AlertDialog的实现

    Android默认的AlertDialog太单调,我们可以通过继承原生的Dialog来实现自定义的Dialog. 本文的自定义Dialog和原生的AlertDialog的创建方式类似,通过一个静态Bu ...

  3. android 自定义AlertDialog(一段)

    java: final AlertDialog dialog = new AlertDialog.Builder(mContext) .create(); dialog.setCancelable(f ...

  4. Android 自定义AlertDialog(退出提示框)

    有时候我们需要在游戏或应用中用一些符合我们样式的提示框(AlertDialog) 以下是我在开发一个小游戏中总结出来的.希望对大家有用. 先上效果图: 下面是用到的背景图或按钮的图片 经过查找资料和参 ...

  5. Android 自定义AlertDialog的写法和弹出软键盘和覆盖状态栏

    private void showMyDialog(int layoutId){ AlertDialog myDialog = new AlertDialog.Builder(context).cre ...

  6. android 自定义alertdialog和取消dialog

    看代码: public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle ...

  7. android 自定义AlertDialog

    xml: alter_dialog_two <?xml version="1.0" encoding="utf-8"?> <LinearLay ...

  8. Android之自定义AlertDialog和PopupWindow实现(仿微信Dialog)

    我们知道,在很多时候,我们都不用Android内置的一些控件,而是自己自定义一些自己想要的控件,这样显得界面更美观. 今天主要是讲自定义AlertDialog和popupWindow的使用,在很多需求 ...

  9. Xamarin.Android 记事本(二)自定义AlertDialog

    导读 1.自定义一个AlertDialog 2.添加一条数据 正文 记事本应当有一个添加功能,这里我打算在右上角放一个item,然后点击这个item弹出一个对话框,输入名称,点击确定跳转到另一个act ...

随机推荐

  1. According to TLD or attribute directive in tag file, attribute items does not accep t any expressions

    According to TLD or attribute directive in tag file, attribute items does not accep t any expression ...

  2. jeecg平台testDatagrid

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...

  3. 在压缩话单中过滤指定IP的一个小脚本

    工作需要,需要过滤出含有指定的IP段的话单,编写的脚本名字叫 filter.sh #!/bin/bash TARGET_PATH=/data/flume/flume_exec_log/Dst_for_ ...

  4. 转载:Kafka 之 中级 原作者:悟性

    Kafka 之 中级 悟性 发表于 3年前 阅读 21353 摘要: Kafka配置介绍,原理介绍及生产者,消费者Java基本使用方法. 1.    配置 Ø  Broker主要配置 参数 默认值 说 ...

  5. openssl req 证书请求及自签名证书

    介绍 openssl req 用于生成证书请求,以让第三方权威机构CA来签发,生成我们需要的证书.req 命令也可以调用x509命令,以进行格式转换及显示证书文件中的text,modulus等信息.如 ...

  6. Rust 之 cargo(项目构建和包管理工具)

    如果食用cargo来进行项目构建: 1. 执行 cargo new hello_cargo --bin ,执行完上面的操作之后,我们切换到hell_cargo目录下,可以看到一个文件(Cargo.to ...

  7. jQuery 自定义网页滚动条样式插件 mCustomScrollbar 的介绍和使用方法(转)

    系统默认的滚动条样式,真的已经看的够恶心了.试想一下,如果在一个很有特色和创意的网页中,出现了一根系统中默认的滚动条样式,会有多么的别扭. 为了自己定义网页中的滚动条的方法,我真的已经找了很久了,就目 ...

  8. Android----输入模式设置

    InputType的参数: 用法:((EditText)findViewById(R.id.edit)).setInputType(InputType.*); int TYPE_CLASS_DATET ...

  9. 用十条命令在一分钟内检查Linux服务器性能[转]

    概述 通过执行以下命令,可以在1分钟内对系统资源使用情况有个大致的了解. uptime dmesg | tail vmstat 1 mpstat -P ALL 1 pidstat 1 iostat - ...

  10. atom介绍

    在公司微信群,看到activate-power-mode插件的效果,很绚丽,才知道github自己出了一个自己的编辑器atom 官网地址 https://atom.io 官网看了下,atom编辑器的特 ...