Android弹出输入提示框--PopupWindow实现

摘要:
现在尝试使用更好的解决方案PopupWindow类来实现1。首先,制作一个弹出式布局,与前一个类似。˃2141524253435454653542。然后制作一个对话框弹出类,这就是游戏。这个东西设置了上面布局中的详细操作,例如按钮监听、弹出窗口功能等。

  前言  之前直接用Dialog实现了弹出对话框。现在尝试用更好地解决方案--PopupWindow类--来实现

  1.首先搞一个弹出框布局,和之前类似。

Android弹出输入提示框--PopupWindow实现第1张

  这样的东西,它的布局是这样:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android: 
 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: 
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: 
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: 
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: 
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实现。

 1 package com.example.jason_jan.自己的包名
 2 
 3 import com.example.jason_jan.自己的项目名.R
 4 import android.app.Activity
 5 import android.content.Context
 6 import android.view.Display
 7 import android.view.LayoutInflater
 8 import android.view.View
 9 import android.view.Window
10 import android.view.WindowManager
11 import android.widget.Button
12 import android.widget.EditText
13 import android.widget.PopupWindow
14 import android.widget.RelativeLayout
15 
16 /**
17  * Created by Jason_Jan on 2017/7/3.
18  */
19 
20 class CreateUserPopWin(mContext: Activity, itemsOnClick: View.OnClickListener?) : PopupWindow() {
21     private val mContext: Context
22 
23     private val view: View
24 
25     private val btn_save_pop: Button
26 
27     var text_name: EditText
28 
29     var text_mobile: EditText
30 
31     var text_info: EditText
32 
33 
34     init {
35 
36         this.mContext = mContext
37 
38         this.view = LayoutInflater.from(mContext).inflate(R.layout.create_user_dialog, null)//这里的layout是之前设置的弹出框布局
39 
40         text_name = view.findViewById(R.id.text_name) as EditText
41         text_mobile = view.findViewById(R.id.text_mobile) as EditText
42         text_info = view.findViewById(R.id.text_info) as EditText
43 
44         btn_save_pop = view.findViewById(R.id.btn_save) as Button
45 
46         // 设置按钮监听
47         btn_save_pop.setOnClickListener(itemsOnClick)
48 
49         // 设置外部可点击
50         this.isOutsideTouchable = true
51 
52 
53         /* 设置弹出窗口特征 */
54         // 设置视图
55         this.contentView = this.view
56 
57         // 设置弹出窗体的宽和高
58         /*
59          * 获取窗口对象及参数对象以修改对话框的布局设置, 可以直接调用getWindow(),表示获得这个Activity的Window
60          * 对象,这样这可以以同样的方式改变这个Activity的属性.
61          */
62         val dialogWindow = mContext.window
63 
64         val m = mContext.windowManager
65         val d = m.defaultDisplay // 获取屏幕宽、高用
66         val p = dialogWindow.attributes // 获取对话框当前的参数值
67 
68         this.height = RelativeLayout.LayoutParams.WRAP_CONTENT
69         this.width = (d.width * 0.8).toInt()
70 
71         // 设置弹出窗体可点击
72         this.isFocusable = true
73 
74     }
75 
76 }

  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: 
    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.最后的测试结果如下

Android弹出输入提示框--PopupWindow实现第2张

Android弹出输入提示框--PopupWindow实现第3张

  6.就是这么简单,背景当然是很难看了。习惯就好。

   

免责声明:文章转载自《Android弹出输入提示框--PopupWindow实现》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇shell命令三剑客之grep命令详解clientdataset的使用下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

误删安卓Android sdk包,打包出现错误解决

手贱删了些sdk以腾电脑空间。结果ionic打包Android出错。 0,SDK Manager AVD Manager都打不开了啊,啊,啊,啊,啊。。。 这个也是需要下载Android SDK Manager,与下面相同。 1,Could not find gradle wrapper within Android SDK. Might need to...

IE中自定义标签使用自封闭格式引发错误!

最近学习IONIC,其中用到了ion-menu-nav-button,由于标签开始和结尾之间没有内容,所以图省事儿使用自封闭标签的写法: <ion-menu-nav-button class="button-icon icon ion-navicon" ng-click="tgMenu();" /> 在chrome和firefox下都没有问题,...

WPF的依赖属性

一、什么是依赖属性 依赖属性就是一种自己可以没有值,并且可以通过绑定从其他数据源获取值。依赖属性可支持WPF中的样式设置、数据绑定、继承、动画及默认值。 将所有的属性都设置为依赖属性并不总是正确的解决方案,具体取决于其应用场景。有时,使用私有字段实现属性的典型方法便能满足要求。MSDN中给出了下面几种应用依赖属性的场景: 1. 希望可在样式中设置属性。 2...

让x86的android模拟器能模拟arm架构系统

2019年展月6日更新: 1、最近发现了一个能在linux下模拟arm的模拟器: xdroid,网址: https://www.linzhuotech.com/index.php/home/index/down.html, 特别好用。直接模拟arm。我的云盘里有其可执行程序。 (这个需要 5G 的根目录空间,不推荐,太大了,这个是linux上运行,不是模拟...

Android实现简单的检测手机自由落体关闭屏幕

实现功能如下:在背景运行app,检测到自由落体状态时,熄灭屏幕,可重复测试。 1. 检测自由落体动作  需要使用到加速度感应器 TYPE_ACCELEROMETER SensorManager mSensorManager; private float mLastX; private float mLastY; private float mLastZ;...

uni-app 知识点

---【uni-app】:   是一个使用vue。js开发所有前端应用的框架,开发者编写一套代码,可发布到ios,android,H5,以及各种小程序,   (微信/支付宝/百度/头条/QQ/钉钉)等多个平台 ---【环境搭建】:   1,安装APP开发版HBuilderX   2,安装微信开发者工具 ---【使用HBuilderX初始化项目】:   1,...