Android弹出输入提示框--PopupWindow实现
前言 之前直接用Dialog实现了弹出对话框。现在尝试用更好地解决方案--PopupWindow类--来实现
1.首先搞一个弹出框布局,和之前类似。

这样的东西,它的布局是这样:
1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:id="@+id/create_user_dialog_view"
4 android:layout_width="fill_parent"
5 android:layout_height="300dp"
6 android:background="@drawable/自己搞背景样式"
7 android:minWidth="200dp"
8 android:orientation="vertical"
9 android:padding="10dp"
10 android:paddingBottom="30dp"
11 android:paddingTop="30dp"
12 android:layout_marginTop="250dp"
13 >
14
15 <EditText
16
17 android:id="@+id/text_name"
18 android:layout_width="fill_parent"
19 android:layout_height="wrap_content"
20 android:background="@drawable/自己搞背景样式"
21 android:hint="编辑框1"
22 android:minHeight="45dp"
23 android:textSize="18sp" />
24
25 <EditText
26 android:id="@+id/text_mobile"
27 android:layout_width="fill_parent"
28 android:layout_height="wrap_content"
29 android:layout_marginTop="5dp"
30 android:background="@drawable/自己搞背景样式"
31 android:hint="编辑框2"
32 android:minHeight="45dp"
33 android:textSize="18sp" />
34
35 <EditText
36 android:id="@+id/text_info"
37 android:layout_width="fill_parent"
38 android:layout_height="wrap_content"
39 android:layout_marginTop="5dp"
40 android:background="@drawable/自己搞背景样式"
41 android:gravity="top|left"
42 android:hint="编辑框3"
43 android:minHeight="145dp"
44 android:textSize="18sp" />
45
46 <Button
47 android:id="@+id/btn_save"
48 android:layout_width="fill_parent"
49 android:layout_height="wrap_content"
50 android:layout_marginTop="5dp"
51 android:background="@自己搞背景样式"
52 android:text="按钮" />
53
54 </LinearLayout>
2.然后搞一个对话框弹出类,就是重头戏了,这个东西设置了上面布局中的细节操作,如按钮监听啊,弹出窗口的特征什么的。用Kotlin实现。
package com.example.jason_jan.自己的包名 import com.example.jason_jan.自己的项目名.R
import android.app.Activity
import android.content.Context
import android.view.Display
import android.view.LayoutInflater
import android.view.View
import android.view.Window
import android.view.WindowManager
import android.widget.Button
import android.widget.EditText
import android.widget.PopupWindow
import android.widget.RelativeLayout /**
* Created by Jason_Jan on 2017/7/3.
*/ class CreateUserPopWin(mContext: Activity, itemsOnClick: View.OnClickListener?) : PopupWindow() {
private val mContext: Context private val view: View private val btn_save_pop: Button var text_name: EditText var text_mobile: EditText var text_info: EditText init { this.mContext = mContext this.view = LayoutInflater.from(mContext).inflate(R.layout.create_user_dialog, null)//这里的layout是之前设置的弹出框布局 text_name = view.findViewById(R.id.text_name) as EditText
text_mobile = view.findViewById(R.id.text_mobile) as EditText
text_info = view.findViewById(R.id.text_info) as EditText btn_save_pop = view.findViewById(R.id.btn_save) as Button // 设置按钮监听
btn_save_pop.setOnClickListener(itemsOnClick) // 设置外部可点击
this.isOutsideTouchable = true /* 设置弹出窗口特征 */
// 设置视图
this.contentView = this.view // 设置弹出窗体的宽和高
/*
* 获取窗口对象及参数对象以修改对话框的布局设置, 可以直接调用getWindow(),表示获得这个Activity的Window
* 对象,这样这可以以同样的方式改变这个Activity的属性.
*/
val dialogWindow = mContext.window val m = mContext.windowManager
val d = m.defaultDisplay // 获取屏幕宽、高用
val p = dialogWindow.attributes // 获取对话框当前的参数值 this.height = RelativeLayout.LayoutParams.WRAP_CONTENT
this.width = (d.width * 0.8).toInt() // 设置弹出窗体可点击
this.isFocusable = true } }
3.然后就是测试这个弹出框类能不能正确执行了。新建一个Activity--我就直接叫做MyDialogTest2
package 自己的项目名 import android.app.Activity
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.util.Log import 自己的项目名.R
import android.view.Gravity
import android.view.View
import 要引用的自己创建的CreateUserPop路径 class MyDialogTest2 : Activity() { var createUserPopWin: CreateUserPopWin?=null override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_my_dialog_test2)
}
/* showEditPopWin*/ fun showEditPopWin(view: View?) {
/*Log.d("TAG","******0")*/
createUserPopWin = CreateUserPopWin(this, onClickListener) createUserPopWin?.showAtLocation(findViewById(R.id.activity_my_dialog_test2), Gravity.CENTER, 0, 0)
//这里的id时这个类对应的布局中的id,不然就像我一样入坑了,下面有具体的布局信息
/* Log.d("TAG","******4")*/
} private val onClickListener
= object : View.OnClickListener {
override fun onClick(v: View?) {
/* Log.d("TAG","******2")*/
when (v?.getId()) {
R.id.btn_save -> { val name1 = createUserPopWin?.text_name?.getText().toString().trim()
val mobile1 = createUserPopWin?.text_mobile?.getText().toString().trim()
val info1 = createUserPopWin?.text_info?.getText().toString().trim() println(name1 + "——" + mobile1 + "——" + info1) createUserPopWin?.dismiss()
}
}
/*Log.d("TAG","******3")*/
}
}
}
4.这里是对应的布局信息,就是两个按钮,点击按钮,弹出对话框
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/activity_my_dialog_test2"
tools:context="对应的类路径--可以不写--"> <Button
android:layout_width="300dp"
android:layout_height="wrap_content" android:layout_marginTop="100dp"
android:layout_centerHorizontal="true"
android:text="测试弹出框"
android:onClick="showEditPopWin"
android:background="@drawable/自己搞一个背景样式"
/> <Button
android:layout_width="300dp"
android:layout_height="wrap_content" android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:text="测试弹出框"
android:onClick="showEditPopWin"
android:background="@drawable/自己搞一个背景样式"
/> </RelativeLayout>
5.最后的测试结果如下


6.就是这么简单,背景当然是很难看了。习惯就好。
Android弹出输入提示框--PopupWindow实现的更多相关文章
- 解决PL/SQL Dev连接Oracle弹出空白提示框
第一次安装Oracle,装在虚拟机中,用PL/SQL Dev连接远程数据库的时候老是弹出空白提示框,网上找了很久,解决方法也很多,可是就是没法解决我这种情况的. 没办法,只能自己研究,经过大概一天时间 ...
- PL/SQL Dev连接Oracle弹出空白提示框的解决方法分享
第一次安装Oracle,装在虚拟机中,用PL/SQL Dev连接远程数据库的时候老是弹出空白提示框,网上找了很久,解决方法也很多,可是就是没法解决我这种情况的. 出现这种问题,解决方法大概有这几种: ...
- 弹出JS提示框
弹出JS提示框Page.ClientScript.RegisterStartupScript(typeof(string), "msg", "<script> ...
- Asp.Net下载页面,并弹出下载提示框
Asp.Net下载页面,并弹出下载提示框.在删除按钮里调用以下方法.
- IE中使用ajaxSubmit上传文件弹出下载提示框
使用jQuery的ajaxSubmit 上传文件时,在IE中会弹出下载提示框: 解决方法:让action返回String类型,而不是ActionView,
- asp.net导出excel并弹出保存提示框
asp.net导出excel并弹出保存提示框 2013-07-12 | 阅:1 转:78 | 分享 腾讯空间 人人网 开心网 新浪微博 腾讯微博 搜狐空间 推荐给朋友 举报 ...
- [UWP]在应用退出时弹出确认提示框
1. 需求 在应用退出时(点击右上角的关闭按钮)弹出一个确认按钮可以说是一个最常见的操作了,例如记事本的"你是否保存": 但这个功能在UWP上居然有点小复杂.这篇文章将解释如何实现 ...
- Android基础TOP4_1:点击物理按钮弹出退出提示框
JAVA: public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedIns ...
- android 弹出日期选择框
DatePickerDialog 在很多时候需要用户去设定时间,不可能让用户去在一个文本框中去输入时间,所以就需要有个日期弹出选择框,而这个框就是DatePickerDialog. 1.在API中的D ...
随机推荐
- [原创]Debian9 从零编译配置Redis4.0
序言 Redis 一.准备工作 1.1 更新系统安装包列表 没啥,就他喵想用个最新的. # apt update 1.2 创建需要使用的目录 创建目录source和web,分别用来放源码和编译后的文件 ...
- Struts2_简单数据验证
在Action 中添加 FieldError if(name == null || !name.equals("admin")){ this.addFieldError(" ...
- webpack前端构建工具学习总结(一)之webpack安装、创建项目
npm是随nodeJs安装包一起安装的包管理工具,能解决NodeJS代码部署上的很多问题: 常见的使用场景有以下几种: 允许用户从NPM服务器下载别人编写的第三方包到本地使用. 允许用户从NPM服务器 ...
- py常见模块
1.系统相关的信息模块: import sys sys.argv 是一个 list,包含所有的命令行参数. sys.stdout sys.stdin sys.stderr 分别表示标准输入输出,错误输 ...
- yum 源搭建
RHEL系统部署网络yum源 配置网络yum源 RHEL系统本身光盘做成的yum源所提供的软件包有限,在实际使用过程中经常会出现缺包的现象,本文中以CentOS源作为替代,CentOS的软件包和RHE ...
- APP专项测试使用到的工具
最近在读<大话APP测试>,我也就是把需要使用的测试点做一个总结,目前是使用的工具进行的整理,后期慢慢把工具使用案例贴出来
- eclipse的一些快捷键
ctrl + 1快速修复 ctrl + d 快速删除 ctrl + F11快速运行 ctrl + m 放大工作区 atl + /注释 ...
- python 下实现window 截图
首先安装PIL库,因为PIL官网没有支持python3.6的PIL库我想在3.X中实现,因此使用pip安装pillow pip install pillow 安装 安装完成后,from PIL imp ...
- 2017.11.8 面向对象分析与设计(UML)---UML的作用及分类
用到的工具 startUML 一些界面操作的说明 蓝色框是用来选择形状的,特别是接口的时候 UML有什么用? `` 有很多人认为,UML的主要用途就是软件设计!也有人认为,如果你不是开发人员,是难以理 ...
- PASCAL VOC数据集分析
http://blog.csdn.net/zhangjunbob/article/details/52769381