当前位置:网站首页>入门级测试Kotlin实现PopWindow弹窗代码
入门级测试Kotlin实现PopWindow弹窗代码
2022-06-22 06:12:00 【须须草】
入门级测试Kotlin PopWindow弹窗代码
文件名称:MainActivity.Kt
package com.example.alert
import android.app.Dialog
import android.content.DialogInterface
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.ViewGroup
import android.widget.Button
import android.widget.GridView
import android.widget.PopupWindow
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
class MainActivity : AppCompatActivity() {
private var btn : Button? = null
private var btnPopupWindow :Button? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//设定按钮
btn = findViewById<Button>(R.id.btnAlert)
btn?.setOnClickListener {
//告警窗口
val alertDialog = AlertDialog.Builder(this@MainActivity)
alertDialog.apply {
setTitle("Hello") //设置标题
setMessage("你说呢?")
setPositiveButton("确定",
DialogInterface.OnClickListener{
_, _ ->
Toast.makeText(this@MainActivity,"你点击了:确定",Toast.LENGTH_SHORT).show()
})
setNegativeButton("取消",
DialogInterface.OnClickListener {
_, _ ->
Toast.makeText(this@MainActivity,"你点击了:取消",Toast.LENGTH_SHORT).show()
})
setIcon(R.drawable.ic_baseline_android_24)
show()
}
}
//弹窗部分开始
btnPopupWindow = findViewById<Button>(R.id.btnPopWindow)
btnPopupWindow?.setOnClickListener {
PopupWindow().apply {
//入口参数配置
contentView = layoutInflater.inflate(R.layout.popwindow_layout,null)
width = ViewGroup.LayoutParams.WRAP_CONTENT
height = ViewGroup.LayoutParams.WRAP_CONTENT
isFocusable = true
//设置按钮
val btnPopupWindow = contentView.findViewById<Button>(R.id.popBtn)
btnPopupWindow.setOnClickListener {
Toast.makeText(this@MainActivity,"退出弹窗!!",Toast.LENGTH_SHORT).show()
dismiss()
}
//显示在按钮的下方
showAsDropDown(it)
}
//这部分注释代码与上面效果一致,除了没添加按钮
// var popupWindow = PopupWindow(
// layoutInflater.inflate(R.layout.popwindow_layout,null),
// ViewGroup.LayoutParams.WRAP_CONTENT,
// ViewGroup.LayoutParams.WRAP_CONTENT,
// true
// )
//
//
// popupWindow.apply {
// showAsDropDown(it)
// }
}
}
}
文件名称:popwindow_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical">
<TextView android:text="你好,我是弹窗!!" android:textSize="23sp" android:textAlignment="center" android:layout_width="match_parent" android:layout_height="wrap_content"/>
<Button android:id="@+id/popBtn" android:text="你好,我是弹窗按钮!!" android:textSize="23sp" android:textAlignment="center" android:layout_width="match_parent" android:layout_height="wrap_content"/>
</LinearLayout>
总结:使用Kotlin可以简化代码,代码量远比Java要跟小一些,使用内置函数apply灵活性强,代码更加容易看懂。
边栏推荐
- 从转载阿里开源项目 Egg.js 技术文档引发的“版权纠纷”,看宽松的 MIT 许可该如何用?
- 单细胞文献学习(part3)--DSTG: deconvoluting spatial transcriptomics data through graph-based AI
- Keil调试时设置断点的高级用法
- 论文实验记录(part1)--Detection ofnatural clusters via S-DBSCAN a Self-tuning version of DBSCAN
- C#中的数组及Foreach遍历
- W800 chip platform enters openharmony backbone
- Subqueries in sqlserver
- Current harmonic suppression strategy of grid connected inverter with PIR controller regulator
- 单球机器人动力学与控制研究
- Single cell paper record (Part11) -- clustermap for multi-scale clustering analysis of spatial gene expression
猜你喜欢

Le contrôle MFC tabctrl modifie la taille de l'étiquette

Research on dynamics and control of single ball robot

DataBricks从开源到商业化踩过的坑

h = key. Hashcode()) ^ (H > > 16) detailed explanation and why the hashcode value should be shifted to the right by 16 bits and XOR with the original hashcode value

关于jinja2 宏定义的小问题

3D asset optimization and vertex data management for performance optimization

MFC TabCtrl 控件修改标签尺寸

Surfer格网文件裁剪

单细胞文献学习(part3)--DSTG: deconvoluting spatial transcriptomics data through graph-based AI

Case analysis of terminal data leakage prevention
随机推荐
单球机器人动力学与控制研究
BinaryFormatter saving and loading game data for unity
单细胞论文记录(part12)--Unsupervised Spatial Embedded Deep Representation of Spatial Transcriptomics
DataBricks从开源到商业化踩过的坑
Improve your game‘s performance
Read and understand the four deep learning methods of supervised learning, unsupervised learning, semi supervised learning and intensive learning
400 hash table (1. sum of two numbers, 454. sum of four numbers II, 383. ransom letter)
机器学习概念梳理(无公式)
关于jinja2 宏定义的小问题
Case analysis of terminal data leakage prevention
生信文献学习(part1)--PRECISE: a ... approach to transfer predictors of drug response from pre-clinical ...
e. Hash & oldcap = = 0 detailed interpretation
论文实验记录(part1)--Detection ofnatural clusters via S-DBSCAN a Self-tuning version of DBSCAN
vcpkg:If you are sure you want to rebuild the above packages, run the command with the --recurse opt
Breakthrough in rich device platform: dayu200 based on rk3568 enters the openharmony 3.1 release trunk
Design input of Oracle project management system
线程池的三大方法
h = key. Hashcode()) ^ (H > > 16) detailed explanation and why the hashcode value should be shifted to the right by 16 bits and XOR with the original hashcode value
雷达导引头伺服系统的建模与仿真
八锁问题详解