Android自定义AlertDialog
常见的一种方法:
[html] view plaincopyprint?

AlertDialog.Builder builder;
AlertDialog alertDialog;
LayoutInflater inflater = getLayoutInflater();
// 添加自定义的布局文件
View layout = LayoutInflater.from(TestOne.this).inflate(
R.layout.dialog, null);
final TextView text = (TextView) layout.findViewById(R.id.tv1);
// 添加点击事件
text.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
text.setText("call");
}
});
builder = new AlertDialog.Builder(TestOne.this);
alertDialog = builder.create();
// 去掉边框的黑色,因为设置的与四周的间距为0
alertDialog.setView(layout, 0, 0, 0, 0);
alertDialog.show();
// 修改大小
WindowManager.LayoutParams params = alertDialog.getWindow()
.getAttributes();
params.width = 350;
params.height = 200;
alertDialog.getWindow().setAttributes(params);
这样 ,重新给它填充自定义的布局视图,但缺乏可扩展性,而且每次使用还得重新定义。
重写AlertDialog类,定义方法:
[html] view plaincopyprint?

/**
* 自定义的对话框
*/
public abstract class MyAlerDialog extends AlertDialog implements
android.view.View.OnClickListener {
protected MyAlerDialog(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
/**
* 布局中的其中一个组件
*/
private TextView txt;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
// 加载自定义布局
setContentView(R.layout.dialog);
// setDialogSize(300, 200);
txt = (TextView) findViewById(R.id.tv1);
txt.setOnClickListener(this);
}
/**
* 修改 框体大小
*
* @param width
* @param height
*/
public void setDialogSize(int width, int height) {
WindowManager.LayoutParams params = getWindow().getAttributes();
params.width = 350;
params.height = 200;
this.getWindow().setAttributes(params);
}
public abstract void clickCallBack();
/**
* 点击事件
*/
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v == txt) {
clickCallBack();
}
}
}
在活动中使用:
[html] view plaincopyprint?

MyAlerDialog mydialog = new MyAlerDialog(this) {
// 重写callback方法
@Override
public void clickCallBack() {
// TODO Auto-generated method stub
btn.setText("call");
}
};
mydialog.show();
自己写的功能就封装了两个,有需要的童鞋可以很容易的扩展。这种方法,显然相对于上一种要有优势得多啦。
Android自定义AlertDialog的更多相关文章
- Android 自定义AlertDialog退出对话框
Android 自定义AlertDialog退出对话框 转 https://blog.csdn.net/wkh11/article/details/53081634在项目中很多时候会出现点击返回键出现 ...
- Android 自定义AlertDialog的实现
Android默认的AlertDialog太单调,我们可以通过继承原生的Dialog来实现自定义的Dialog. 本文的自定义Dialog和原生的AlertDialog的创建方式类似,通过一个静态Bu ...
- android 自定义AlertDialog(一段)
java: final AlertDialog dialog = new AlertDialog.Builder(mContext) .create(); dialog.setCancelable(f ...
- Android 自定义AlertDialog(退出提示框)
有时候我们需要在游戏或应用中用一些符合我们样式的提示框(AlertDialog) 以下是我在开发一个小游戏中总结出来的.希望对大家有用. 先上效果图: 下面是用到的背景图或按钮的图片 经过查找资料和参 ...
- Android 自定义AlertDialog的写法和弹出软键盘和覆盖状态栏
private void showMyDialog(int layoutId){ AlertDialog myDialog = new AlertDialog.Builder(context).cre ...
- android 自定义alertdialog和取消dialog
看代码: public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle ...
- android 自定义AlertDialog
xml: alter_dialog_two <?xml version="1.0" encoding="utf-8"?> <LinearLay ...
- Android之自定义AlertDialog和PopupWindow实现(仿微信Dialog)
我们知道,在很多时候,我们都不用Android内置的一些控件,而是自己自定义一些自己想要的控件,这样显得界面更美观. 今天主要是讲自定义AlertDialog和popupWindow的使用,在很多需求 ...
- Xamarin.Android 记事本(二)自定义AlertDialog
导读 1.自定义一个AlertDialog 2.添加一条数据 正文 记事本应当有一个添加功能,这里我打算在右上角放一个item,然后点击这个item弹出一个对话框,输入名称,点击确定跳转到另一个act ...
随机推荐
- 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 ...
- jeecg平台testDatagrid
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...
- 在压缩话单中过滤指定IP的一个小脚本
工作需要,需要过滤出含有指定的IP段的话单,编写的脚本名字叫 filter.sh #!/bin/bash TARGET_PATH=/data/flume/flume_exec_log/Dst_for_ ...
- 转载:Kafka 之 中级 原作者:悟性
Kafka 之 中级 悟性 发表于 3年前 阅读 21353 摘要: Kafka配置介绍,原理介绍及生产者,消费者Java基本使用方法. 1. 配置 Ø Broker主要配置 参数 默认值 说 ...
- openssl req 证书请求及自签名证书
介绍 openssl req 用于生成证书请求,以让第三方权威机构CA来签发,生成我们需要的证书.req 命令也可以调用x509命令,以进行格式转换及显示证书文件中的text,modulus等信息.如 ...
- Rust 之 cargo(项目构建和包管理工具)
如果食用cargo来进行项目构建: 1. 执行 cargo new hello_cargo --bin ,执行完上面的操作之后,我们切换到hell_cargo目录下,可以看到一个文件(Cargo.to ...
- jQuery 自定义网页滚动条样式插件 mCustomScrollbar 的介绍和使用方法(转)
系统默认的滚动条样式,真的已经看的够恶心了.试想一下,如果在一个很有特色和创意的网页中,出现了一根系统中默认的滚动条样式,会有多么的别扭. 为了自己定义网页中的滚动条的方法,我真的已经找了很久了,就目 ...
- Android----输入模式设置
InputType的参数: 用法:((EditText)findViewById(R.id.edit)).setInputType(InputType.*); int TYPE_CLASS_DATET ...
- 用十条命令在一分钟内检查Linux服务器性能[转]
概述 通过执行以下命令,可以在1分钟内对系统资源使用情况有个大致的了解. uptime dmesg | tail vmstat 1 mpstat -P ALL 1 pidstat 1 iostat - ...
- atom介绍
在公司微信群,看到activate-power-mode插件的效果,很绚丽,才知道github自己出了一个自己的编辑器atom 官网地址 https://atom.io 官网看了下,atom编辑器的特 ...