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 ...
随机推荐
- 【原创+亲测可用】JS如何区分微信浏览器、QQ浏览器和QQ内置浏览器
1.原理: 通过不同移动端的ua弹窗 获取user-agent 参数包含的信息,进行判断浏览器类型 在Android上 QQ内置环境的ua中有关键字 MQQBrowser, 并且后面包含一个[空白符+ ...
- 【RS】List-wise learning to rank with matrix factorization for collaborative filtering - 结合列表启发排序和矩阵分解的协同过滤
[论文标题]List-wise learning to rank with matrix factorization for collaborative filtering (RecSys '10 ...
- mysql--SQL编程(基础知识) 学习笔记1
1.数据库应用类型分类: 一般来说,可将数据库的应用类型分为OLTP(OnLine TransactionProcessing ,联机事务处理)和OLAP(OnLine Analysis Proces ...
- Apache 日志设置不记录指定文件类型的方法和日志轮
Apache日志精准的记录了Web访问的记录,但对于访问量很大的站来说,日志文件过大对于分析和保存很不方便.可以在http.conf(或虚拟主机设置文件httpd-vhosts.conf)中进行设置, ...
- webpack window 使用sass来编译css样式
1.执行安装: npm install sass-loader --save-dev (此处不行的话就换上npm install node-sass) 2.稍微修改一下config,删掉我们先前添加的 ...
- SpringBoot中Redis的set、map、list、value、实体类等基本操作介绍
今天给大家介绍一下SpringBoot中Redis的set.map.list.value等基本操作的具体使用方法 上一节中给大家介绍了如何在SpringBoot中搭建Redis缓存数据库,这一节就针对 ...
- 【struts2】Struts2的运行流程
1)前提条件 在讲解流程之前,假设我们已经建立了的一个名为strutsDeepen的web工程,该工程仅仅实现了简单的用户登陆与欢迎界面.具体的实现为: 在web.xml中配置了Struts2的过滤器 ...
- 兼容chrome和ie的wav音乐播放(Ie7 Ie8 Ie9 均测试过 )
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 全面拥抱移动测试,Mobile JSON Wire Protocol Specification文档翻译
Selenium3已经宣布不支持移动化测试.对于老牌测试工具selenium来说这是以退为进,因为移动自动化测试工具的标准还在selenium团队手上. 本文轻度翻译了这个标准,看得懂的人不用翻译也能 ...
- LIGHT OJ 1199 - Partitioning Game
传送门 1199 - Partitioning Game PDF (English) problem=1199" style="color:rgb(79,107,114)&q ...