上午写了一篇博文,介绍了如何定义从屏幕底部弹出PopupWindow,写完之后,突然想起之前写过自定义内容显示的弹出框,就随手写了两个实例,分享出来:

第一种实现方式:继承Dialog

1.1 线定义弹出框要显示的内容:create_user_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/create_user_dialog_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/dialog_load_bg"
android:minWidth="200dp"
android:orientation="vertical"
android:padding="10dp"
android:paddingBottom="30dp"
android:paddingTop="30dp"> <EditText
android:id="@+id/text_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/edit_bg"
android:hint="姓名"
android:minHeight="45dp"
android:textSize="18sp" /> <EditText
android:id="@+id/text_mobile"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:background="@drawable/edit_bg"
android:hint="手机号"
android:minHeight="45dp"
android:textSize="18sp" /> <EditText
android:id="@+id/text_info"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:background="@drawable/edit_bg"
android:gravity="top|left"
android:hint="个性签名"
android:minHeight="145dp"
android:textSize="18sp" /> <Button
android:id="@+id/btn_save_pop"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="保存" /> </LinearLayout>

1.2 定义要弹出的Dialog

public class CreateUserDialog extends Dialog {

    /**
* 上下文对象 *
*/
Activity context; private Button btn_save; public EditText text_name; public EditText text_mobile; public EditText text_info; private View.OnClickListener mClickListener; public CreateUserDialog(Activity context) {
super(context);
this.context = context;
} public CreateUserDialog(Activity context, int theme, View.OnClickListener clickListener) {
super(context, theme);
this.context = context;
this.mClickListener = clickListener;
} @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 指定布局
this.setContentView(R.layout.create_user_dialog); text_name = (EditText) findViewById(R.id.text_name);
text_mobile = (EditText) findViewById(R.id.text_mobile);
text_info = (EditText) findViewById(R.id.text_info); /*
* 获取圣诞框的窗口对象及参数对象以修改对话框的布局设置, 可以直接调用getWindow(),表示获得这个Activity的Window
* 对象,这样这可以以同样的方式改变这个Activity的属性.
*/
Window dialogWindow = this.getWindow(); WindowManager m = context.getWindowManager();
Display d = m.getDefaultDisplay(); // 获取屏幕宽、高用
WindowManager.LayoutParams p = dialogWindow.getAttributes(); // 获取对话框当前的参数值
// p.height = (int) (d.getHeight() * 0.6); // 高度设置为屏幕的0.6
p.width = (int) (d.getWidth() * 0.8); // 宽度设置为屏幕的0.8
dialogWindow.setAttributes(p); // 根据id在布局中找到控件对象
btn_save = (Button) findViewById(R.id.btn_save); // 为按钮绑定点击事件监听器
btn_save.setOnClickListener(mClickListener); this.setCancelable(true);
}
}

1.3 调用弹出框:

    public void showEditDialog(View view) {
createUserDialog = new CreateUserDialog(this,R.style.loading_dialog,onClickListener);
createUserDialog.show();
}
 private View.OnClickListener onClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) { switch (v.getId()) {
case R.id.btn_save: String name = createUserDialog.text_name.getText().toString().trim();
String mobile = createUserDialog.text_mobile.getText().toString().trim();
String info = createUserDialog.text_info.getText().toString().trim(); System.out.println(name+"——"+mobile+"——"+info);
break;
}
}
};

第二种实现方式:继承PopupWindow

  2.1 定义弹出框布局文件,和1.1定义的一致

2.2 定义要弹出的PopupWindow

public class CreateUserPopWin extends PopupWindow {

    private Context mContext;

    private View view;

    private Button btn_save_pop;

    public EditText text_name;

    public EditText text_mobile;

    public EditText text_info;

    public CreateUserPopWin(Activity mContext, View.OnClickListener itemsOnClick) {

        this.mContext = mContext;

        this.view = LayoutInflater.from(mContext).inflate(R.layout.create_user_pop, null);

        text_name = (EditText) view.findViewById(R.id.text_name);
text_mobile = (EditText) view.findViewById(R.id.text_mobile);
text_info = (EditText) view.findViewById(R.id.text_info); btn_save_pop = (Button) view.findViewById(R.id.btn_save_pop); // 设置按钮监听
btn_save_pop.setOnClickListener(itemsOnClick); // 设置外部可点击
this.setOutsideTouchable(true); /* 设置弹出窗口特征 */
// 设置视图
this.setContentView(this.view); // 设置弹出窗体的宽和高
/*
* 获取圣诞框的窗口对象及参数对象以修改对话框的布局设置, 可以直接调用getWindow(),表示获得这个Activity的Window
* 对象,这样这可以以同样的方式改变这个Activity的属性.
*/
Window dialogWindow = mContext.getWindow(); WindowManager m = mContext.getWindowManager();
Display d = m.getDefaultDisplay(); // 获取屏幕宽、高用
WindowManager.LayoutParams p = dialogWindow.getAttributes(); // 获取对话框当前的参数值 this.setHeight(RelativeLayout.LayoutParams.WRAP_CONTENT);
this.setWidth((int) (d.getWidth() * 0.8)); // 设置弹出窗体可点击
this.setFocusable(true); }
}

2.3 调用该弹框组件

public void showEditPopWin(View view) {

        createUserPopWin = new CreateUserPopWin(this,onClickListener);

        createUserPopWin.showAtLocation(findViewById(R.id.main_view), Gravity.CENTER, 0, 0);
}
 private View.OnClickListener onClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) { switch (v.getId()) {
case R.id.btn_save_pop: String name1 = createUserPopWin.text_name.getText().toString().trim();
String mobile1 = createUserPopWin.text_mobile.getText().toString().trim();
String info1 = createUserPopWin.text_info.getText().toString().trim(); System.out.println(name1+"——"+mobile1+"——"+info1); createUserPopWin.dismiss();
break;
}
}
};

源码下载地址(免费):http://download.csdn.net/detail/zuiwuyuan/9075977

Android 自定义界面的弹出框(可输入数据)的更多相关文章

  1. android 三种弹出框之一PopupWindow

    PopupWindow 在android的弹出框我目前了解到的是有三种:AlertDialog,PopupWindow,Activity伪弹框, AlertDialog太熟悉了,这里就不介绍了 就先看 ...

  2. Android窗口为弹出框样式

    1.XML android:theme="@android:style/Theme.Dialog <?xml version="1.0" encoding=&quo ...

  3. android开发学习 ------- 弹出框

    这是一种方法,是我觉得简单易懂代码量较少的一种: /* 创建AlertDialog对象并显示 */ final AlertDialog alertDialog = new AlertDialog.Bu ...

  4. Android 开发笔记 “弹出框”

    AlertDialog.Builder builder = new AlertDialog.Builder(Activity.this); builder.setMessage("Are y ...

  5. 【Android】各式各样的弹出框与对菜单键、返回键的监听

    Android自带各式各样的弹出框.弹出框也是安卓主要的组件之中的一个.同一时候安卓程序能够对菜单键.返回键的监听.但在安卓4.0之后就禁止对Home键的屏蔽与监听,强制保留为系统守护按键.假设非要对 ...

  6. 使用DIV弹出框的代码示例,备忘。

    1.思路 使用DIV模拟弹出框,一共用三个div: divWindow: 原来的界面内容区域 divLogin:要弹出的内容区域 divBackground:给弹出内容区域做个遮罩的区域. 点击 “请 ...

  7. android标题栏上面弹出提示框(二) PopupWindow实现,带动画效果

    需求:上次用TextView写了一个从标题栏下面弹出的提示框.android标题栏下面弹出提示框(一) TextView实现,带动画效果,  总在找事情做的产品经理又提出了奇葩的需求.之前在通知栏显示 ...

  8. android标题栏下面弹出提示框(一) TextView实现,带动画效果

    产品经理用的是ios手机,于是android就走上了模仿的道路.做这个东西也走了一些弯路,写一篇博客放在这里,以后自己也可用参考,也方便别人学习. 弯路: 1.刚开始本来用PopupWindow去实现 ...

  9. Android 学习笔记之AndBase框架学习(二) 使用封装好的进度框,Toast框,弹出框,确认框...

    PS:渐渐明白,在实验室呆三年都不如在企业呆一年... 学习内容: 1.使用AbActivity内部封装的方法实现进度框,Toast框,弹出框,确认框...   AndBase中AbActivity封 ...

随机推荐

  1. day38 11-Spring的Bean的属性的注入:对象属性

    package cn.itcast.spring3.demo5; public class Person { private String name;//人的名字 private Car2 car2; ...

  2. 利用Git搭建自动部署的Laravel环境 - 钟晨宇的博客 - CSDN博客

    目标:服务器上搭建Laravel环境,本地使用IDE进行开发,使用Homestead做本地调试环境,代码提交后自动部署到服务器Root目录下. 下面是整个流程的示意图:  1. 准备工作,搭建LNMP ...

  3. 模拟20 题解(waiting)

    留坑待填 T2 #include<cstdio> #include<vector> #include<cstring> #include<iostream&g ...

  4. 二分查找(BinSearch)的Javascript实现

    二分查找 解析:二分查找,也为折半查找.对于一个从小到大排列的有序数组,首先要找到一个中间值,通过与中间值比较,大的放又,小的放在左边.再在两边中寻找中间值,持续以上操作,直到找到所在位置为止. 1. ...

  5. Excel怎么增加撤销操作的次数?Excel增加可撤销次数教程

    Excel怎么增加撤销操作的次数?Excel增加可撤销次数教程 在Excel的使用中,返回上一步是经常用到的一个工具,当数据填写有误需要查看之前的内容时,一般会通过"Ctrl Z" ...

  6. 【JZOJ5081】【GDSOI2017第三轮模拟】Travel Plan 背包问题+双指针+树的dfs序

    题面 100 注意到ban的只会是一个子树,所以我们把原树转化为dfs序列. 然后题目就转化为,询问一段ban的区间,之后的背包问题. 比赛的时候,我想到这里,于是就开始想区间合并,于是搞了线段树合并 ...

  7. CSS基础教程:群组化选择器

    常常我们的CSS 样式中会有好几个地方需要使用到相同的设定时,一个一个分开写会是一件满累人的工作,重覆性太高且显得冗长,更不好管理....在CSS 语法的基本设定中,就可以把这几个相同设定的选择器合并 ...

  8. 使用Fiddler抓取到的“姐夫酷”API接口

    下午本来准备抓取些网页视频地址,做一个小的视频app,用来学习ijkplayer,无意中发现了一个app--姐夫酷,这是一个很简单的网页,它也有相应的一个比较简单的android app. 于是心血来 ...

  9. Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第二十一章:环境光遮蔽(AMBIENT OCCLUSION)

    原文:Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第二十一章:环境光遮蔽(AMBIENT OCCLUSION) 学习目标 ...

  10. Leetcode830.Positions of Large Groups较大分组的位置

    在一个由小写字母构成的字符串 S 中,包含由一些连续的相同字符所构成的分组. 例如,在字符串 S = "abbxxxxzyy" 中,就含有 "a", " ...