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 ...
随机推荐
- EF--payload or not
负载加载非负载加载适用于多对多场境. 一.非负载(payload-free)加载 1.1创建表 create table Album ( AlbumId ,), AlbumName ) ) creat ...
- time和datetime模块
在Python中,通常有这几种方式来表示时间: 1)时间戳 2)格式化的时间字符串 3)元组(struct_time)共九个元素. 由于Python的time模块实现主要调用C库,所以各个平台可能有 ...
- python3绘图示例5(基于matplotlib:正弦图等)
#!/usr/bin/env python# -*- coding:utf-8 -*- import numpy as npimport pylab as pyimport matplotlib as ...
- Verilog分频器的设计
大三都要结束了,才发现自己太多东西没深入学习. 对于偶分频:(计数到分频数的一半就翻转) 注: 图中只用了一个计数器,当然也可以用多个: 图中只计数到需要分频的一半,当然也可计数到更多: 图中从第一个 ...
- 是否应该提供一个dao.insertIgnoreNull ? (像updateIgnoreNull一样)
是否应该提供一个dao.insertIgnoreNull ? (像updateIgnoreNull一样) 发布于 406天前 作者 SayingCode 153 次浏览 复制 上一个帖子 ...
- Android(java)学习笔记64:Android权限大全
访问登记属性 android.permission.ACCESS_CHECKIN_PROPERTIES读取或写入登记check-in数据库属性表的权限 获取错略位置 android.permissio ...
- PHP获取当前页面完整路径URL
//PHP获取当前页面完整路径URL 1 <?php function getFullUrl(){ # 解决通用问题 $requestUri = ''; if (isset($_SERVER[' ...
- N76E003---输入捕获
输入捕获 根据芯片手册,定时器2可以作为输入捕获使用,设置非常简单,官方也提供了宏给我们使用 void Time2_cap_init(void) { /******* 输入捕获CF设置 ******* ...
- C# 委托知识总结【转】
1.什么是委托,为什么要使用委托 我正在埋头苦写程序,突然想喝水,但是又不想自己去掉杯水而打断自己的思路,于是我就想让女朋友去给我倒水.她去给我倒水,首先我得让她知道我想让她干什么,通知她之后我可以继 ...
- react中实现原生enter/回车事件及antdesign组件实现方式
先直接上核心代码: this.goToHomePage换成自己逻辑 自己写的时候直接把this.goToHmoPage()换成自己的逻辑就行了,还有注意一点的是: 需要传个空函数,不然会报错 在com ...