android开发学习 ------- 弹出框
这是一种方法,是我觉得简单易懂代码量较少的一种:
/* 创建AlertDialog对象并显示 */
final AlertDialog alertDialog = new AlertDialog.Builder(LoginActivity.this).create();
alertDialog.show();
/* 添加对话框自定义布局 */
alertDialog.setContentView(R.layout.dialog_login);
/* 获取对话框窗口 */
Window window = alertDialog.getWindow();
/* 设置显示窗口的宽高 */
window.setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
/* 设置窗口显示位置 */
window.setGravity(Gravity.CENTER);
/* 通过window找布局里的控件 */
window.findViewById(R.id.login).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.e("log", "进入onclick函数体内");
// 隐藏对话框
alertDialog.dismiss();
//自己进行其他的处理
}
});
R.layout.dialog_login.xml (自己想要的样子需要在布局中设置好)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="vertical"
android:background="@drawable/dialog_corner_bg"
android:layout_margin="20dp"
xmlns:android="http://schemas.android.com/apk/res/android"> <Button
android:padding="5dp"
android:layout_marginBottom="5dp"
android:id="@+id/login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="@null"
android:text="OK"
android:textColor="@color/btn_txt_blue"
android:textSize="25sp"
android:textStyle="bold" /> </LinearLayout>
dialog_corner_bg.xml (对话框的背景)
<?xml version="1.0" encoding="utf-8"?>
<!-- 用于设置信息对话框的圆角 -->
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="20dip"></corners>
<solid android:color="@color/white"></solid>
</shape>
效果如下图:
************************************************************************************************
这是另外一种方法:(需要设置合适的主题)
final Dialog dialog2 = new Dialog(LoginActivity.this, R.style.ActionSheetDialogStyle);
LinearLayout view = (LinearLayout) LayoutInflater.from(LoginActivity.this).inflate(R.layout.dialog_forgotpwd, null);
TextView dismiss = (TextView) view.findViewById(R.id.btn_dismiss);
TextView forgotPin = (TextView) view.findViewById(R.id.btn_forgotpin);
TextView forgotPassword = (TextView) view.findViewById(R.id.btn_forgotpassword);
dismiss.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog2.dismiss();
}
});
forgotPin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) { }
});
forgotPassword.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) { }
}); dialog2.setContentView(view);
Window dialogWindow = dialog2.getWindow();
dialogWindow.setGravity(Gravity.BOTTOM);
WindowManager.LayoutParams lp = dialogWindow.getAttributes();
lp.y = 30; //距离底部的高度
dialogWindow.setAttributes(lp);
dialog2.show();
style.xml
<style name="ActionSheetDialogStyle" parent="@android:style/Theme.Dialog">
<!-- 背景透明 -->
<item name="android:windowBackground">@drawable/email_dialogfrag_bg</item>
<item name="android:windowContentOverlay">@null</item>
<!-- 浮于Activity之上 -->
<item name="android:windowIsFloating">true</item>
<!-- 边框 -->
<item name="android:windowFrame">@null</item>
<!-- Dialog以外的区域模糊效果 -->
<item name="android:backgroundDimEnabled">true</item>
<!-- 无标题 -->
<item name="android:windowNoTitle">true</item>
<!-- 半透明 -->
<item name="android:windowIsTranslucent">true</item>
<item name="android:color">@color/white</item>
</style>
效果如下图:
android开发学习 ------- 弹出框的更多相关文章
- Android 开发笔记 “弹出框”
AlertDialog.Builder builder = new AlertDialog.Builder(Activity.this); builder.setMessage("Are y ...
- android 开发 制作弹出等待进度条
技术点: dialog:ProgressBar:animated-rotate: 弹出框: import com.carspeak.client.R; import android.app.Dialo ...
- android 三种弹出框之一PopupWindow
PopupWindow 在android的弹出框我目前了解到的是有三种:AlertDialog,PopupWindow,Activity伪弹框, AlertDialog太熟悉了,这里就不介绍了 就先看 ...
- Android 自定义界面的弹出框(可输入数据)
上午写了一篇博文,介绍了如何定义从屏幕底部弹出PopupWindow,写完之后,突然想起之前写过自定义内容显示的弹出框,就随手写了两个实例,分享出来: 第一种实现方式:继承Dialog 1.1 线定义 ...
- windows phone 8.1开发:(消息弹出框)强大的ContentDialog
原文出自:http://www.bcmeng.com/contentdialog/ 在应用开发中我们必不可少的会使用到消息框,windows phone8中的messagebox在windows ph ...
- Android窗口为弹出框样式
1.XML android:theme="@android:style/Theme.Dialog <?xml version="1.0" encoding=&quo ...
- 【Android】各式各样的弹出框与对菜单键、返回键的监听
Android自带各式各样的弹出框.弹出框也是安卓主要的组件之中的一个.同一时候安卓程序能够对菜单键.返回键的监听.但在安卓4.0之后就禁止对Home键的屏蔽与监听,强制保留为系统守护按键.假设非要对 ...
- Android 学习笔记之AndBase框架学习(二) 使用封装好的进度框,Toast框,弹出框,确认框...
PS:渐渐明白,在实验室呆三年都不如在企业呆一年... 学习内容: 1.使用AbActivity内部封装的方法实现进度框,Toast框,弹出框,确认框... AndBase中AbActivity封 ...
- android标题栏下面弹出提示框(一) TextView实现,带动画效果
产品经理用的是ios手机,于是android就走上了模仿的道路.做这个东西也走了一些弯路,写一篇博客放在这里,以后自己也可用参考,也方便别人学习. 弯路: 1.刚开始本来用PopupWindow去实现 ...
随机推荐
- 【Mongodb教程 第七课 】MongoDB 查询文档
find() 方法 要从MongoDB 查询集合数据,需要使用MongoDB 的 find() 方法. 语法 基本的find()方法语法如下 >db.COLLECTION_NAME.find() ...
- Centos 6.4 实际工作环境搭建(LNMP)
基本配置 服务器IP设置.编辑网卡配置文件,命令: vi /etc/sysconfig/network-scripts/ifcfg-eth0 注:ifcfg-eth0参数 TYPE=Ethernet ...
- HDU 1031.Design T-Shirt【结构体二次排序】【8月21】
Design T-Shirt Problem Description Soon after he decided to design a T-shirt for our Algorithm Board ...
- 计算机体系结构的铁律(iron law)
计算机体系结构的铁律可由下面公式来描写叙述: 从Programmer的角度来看,处理器的性能就是运行程序的耗费的时间.所以用Time/Program来刻画处理器性能.而这个简单的公式背后是有很丰富的内 ...
- onDestroy强制退出后,process crash的处理
from http://bbs.9ria.com/thread-248722-1-1.html 一般情况,我们在执行测试的过程中都会调用tearDwon方法,以Robotium为例,我们在te ...
- Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyExce
Error setting property values ; nested exception is org.springframework.beans.NotWritablePropertyExc ...
- 《31天成为IT服务达人》--做事篇(第四章)之如何找目标
前面介绍了什么是IT服务.以下几章将介绍IT服务该怎么做.在聊怎么做之前.想起几句流行的告白和准备入行IT服务事业的朋友共勉. 当你的才华 还撑不起你的野心时 就应该静下心来 学习 --- 当你 ...
- 两大数相乘 -- javascript 实现
(function (){ var addLarge = function(n1,n2){ var carry = 0; var ret = ""; n1=n1.toString( ...
- android stdio 异常
1.android studio gradle project sync failed File -> Settings 搜索Gradle 2.eqmu-system-i386未响应 分辨率 ...
- java生成随机汉字
方法一: public static char getRandomChar() { return (char) (0x4e00 + (int) (Math.random() * (0x9fa5 - 0 ...