Android 开发使用PopupWindow实现弹出警告框的复用类示例
作者:LIFE_R 发布时间:2022-04-07 03:43:42
标签:Android,PopupWindow,弹出警告框
本文实例讲述了Android 开发使用PopupWindow实现弹出警告框的复用类。分享给大家供大家参考,具体如下:
Android开发中相信下图所示界面大家都不陌生,该种弹出框的使用频率也是极高的,所以我专门谢了个类用于方便的弹出该界面。并把确定或取消后的逻辑通过抽象方法的方式让用户自己实现,大大提高了开发效率。下面是该类:
package com.***.popupwindow;
import ******;
public abstract class MyPopupWindow {
private PopupWindow popupWindow;
private Activity context;
private String content;
private String positiveWord = "确定";
private String negativeWord = "取消";
/**
* 构造函数
*
* @param context
*/
public MyPopupWindow(Activity context) {
this.context = context;
}
/**
* 显示警示框
*/
public void show() {
View popView = View.inflate(context, R.layout.popup, null);
popupWindow = new PopupWindow(context);
popupWindow.setHeight(400);
popupWindow.setWidth(700);
popupWindow.setOutsideTouchable(true);
popupWindow.setFocusable(true);
popupWindow.setContentView(popView);
popupWindow.showAtLocation(context.getWindow().getDecorView(), Gravity.CENTER, 0, 0);
TextView tv_pop_text = (TextView) popView.findViewById(R.id.tv_pop_text);
tv_pop_text.setText(content);
Button bt_pop_sure = (Button) popView.findViewById(R.id.bt_pop_sure);
bt_pop_sure.setText(positiveWord);
bt_pop_sure.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sureClick();
}
});
Button bt_pop_cancel = (Button) popView.findViewById(R.id.bt_pop_cancel);
bt_pop_cancel.setText(negativeWord);
bt_pop_cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
cancelClick();
}
});
}
/**
* 确定键按下后执行
*/
public abstract void sureClick();
/**
* 取消键按下后执行
*/
public abstract void cancelClick();
/**
* 为警示设置警示内容
*
* @param content
*/
public void setContent(String content) {
this.content = content;
}
/**
* 设置确定键文字
*
* @param positiveWord
*/
public void setPositiveWord(String positiveWord) {
this.positiveWord = positiveWord;
}
/**
* 设置取消键文字
*
* @param negativeWord
*/
public void setNegativeWord(String negativeWord) {
this.negativeWord = negativeWord;
}
/**
* 手动取消警示框
*/
public void dismiss() {
popupWindow.dismiss();
}
}
其中弹出框用到的布局popup.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:background="@android:color/white"
android:orientation="vertical">
<TextView
android:id="@+id/tv_pop_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"/>
<TextView
android:layout_width="match_parent"
android:layout_height="1px"
android:background="@android:color/darker_gray"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/bt_pop_sure"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:layout_weight="1"/>
<TextView
android:layout_width="1px"
android:layout_height="match_parent"
android:background="@android:color/darker_gray"/>
<Button
android:id="@+id/bt_pop_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
下面简单的使用一下:在界面放一个按钮,按钮点击后弹出警告框。代码如下:
package com.toprs.popupwindow;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.SeekBar;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private PopupWindow popupWindow;
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MyPopupWindow myPopupWindow = new MyPopupWindow(MainActivity.this) {
@Override
public void sureClick() {
Toast.makeText(MainActivity.this, "确定", Toast.LENGTH_SHORT).show();
}
@Override
public void cancelClick() {
Toast.makeText(MainActivity.this, "取消", Toast.LENGTH_SHORT).show();
}
};
myPopupWindow.setContent("确定退出?");
myPopupWindow.show();
}
});
}
}
即如下效果:
So,以后使用只需要简单调用几句代码就好了!
希望本文所述对大家Android程序设计有所帮助。
来源:https://blog.csdn.net/wangtaocsdn/article/details/71330670


猜你喜欢
- Java集合ArrayDeque类实例分析前言ArrayDeque类是双端队列的实现类,类的继承结构如下面,继承自AbastractColl
- Springboot使用test无法启动test无法启动,遇到java.lang.IllegalStateException: Unable
- 虽然现在硬盘越来越大,但是清理垃圾还是必要的。这时我们往往需要一个获取文件夹所占空间大小的功能,从而判断垃圾文件的位置。这个时候,我们常用的
- 这几天在排查一个堆外内存泄漏的问题时看到很多人都提到了gperftools这个神器,想要尝试一下结果发现它对macOS的支持不太友好。而且大
- 本文实例讲述了Android控件之TabHost用法。分享给大家供大家参考。具体如下:以下通过TabHost实现android选项卡。mai
- 在游戏开发中,主角需要通过跑地图来通关升级,本章主要介绍主角的移动和摄像跟随的操作。主角移动角色位移通过主角的骨骼动画控制(后续文章会详细介
- 话不多说,下面来直接看示例代码具体代码:DayOfWeek4Birthday.javapackage com.gua;import java
- 前言本文主要给大家介绍了关于Java读取二进制文件的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧。读Hex写CS
- redisson的几大特性相信看了这个标题的同学,对这个问题以已经非常不陌生了,信手拈来redisson的几大特性:可重入性【多个业务线同一
- SpringFramework5.0是自2013年12月版本4发布之后SpringFramework的第一个主发行版。SpringFrame
- 一、引入先给出一个Num类的定义internal class Num{ public static int odd = 5000
- 一、泛型1.1 泛型类的定义// 1. 尖括号 <> 是泛型的标志// 2. E 是类型变量(Type Variable),变量
- 一 :问题背景问题:当查询接口较复杂时候,数据的获取都需要[远程调用],必然需要花费更多的时间。 假如查询文章详情页面,需要如下标注的时间才
- 1、接口:一种把类抽象的更彻底,接口里只能包含抽象方法的“特殊类”。接口不关心类的内部状态数据,定义的是一批类所遵守的规范。(它只规定这批类
- 目录一、首先导入生成二维码和微信支付环境二、在application.yml文件配置微信所有需的基本配置1.导入2.创建MyWXPayCon
- 目前很多网页都有滑动验证,目的就是防止不良爬虫扒他们网站的数据,我这次本着学习的目的使用Java和selenium学习解决滑动验证的问题,前
- ////////////////////////////
- 1.小程序推送信息列如我们去餐厅等位有预约提醒,剩余桌数首先申请一个小程序,微信开放平台:小程序2.申请小程序信息,申请信息模板appid&
- 前言本文主要给大家介绍的是关于Java对xls文件进行读写操作的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍:wi
- Java实现驼峰、下划线互转1.使用 Guava 实现先引入相关依赖<dependency> <