Android 开发使用PopupWindow实现弹出警告框的复用类示例

 更新时间:2020年05月21日 10:31:10   作者:LIFE_R  
这篇文章主要介绍了Android 开发使用PopupWindow实现弹出警告框的复用类,结合实例形式分析了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相关内容感兴趣的读者可查看本站专题:《Android控件用法总结》、《Android开发入门与进阶教程》、《Android视图View技巧总结》、《Android编程之activity操作技巧总结》、《Android数据库操作技巧总结》及《Android资源操作技巧汇总

希望本文所述对大家Android程序设计有所帮助。

相关文章

  • Android 优化之卡顿优化的实现

    Android 优化之卡顿优化的实现

    这篇文章主要介绍了Android 优化之卡顿优化的实现,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-07-07
  • Android编程实现抽屉效果的方法详解

    Android编程实现抽屉效果的方法详解

    这篇文章主要介绍了Android编程实现抽屉效果的方法,结合具体实例形式分析了Android实现抽屉效果的具体步骤与相关操作技巧,需要的朋友可以参考下
    2017-05-05
  • Android RecyclerView实现吸顶动态效果流程分析

    Android RecyclerView实现吸顶动态效果流程分析

    RecyclerView是Android一个更强大的控件,其不仅可以实现和ListView同样的效果,还有优化了ListView中的各种不足。其可以实现数据纵向滚动,也可以实现横向滚动(ListView做不到横向滚动)。接下来讲解RecyclerView的用法
    2022-12-12
  • android获取联系人示例分享

    android获取联系人示例分享

    这篇文章主要介绍了android获取联系人示例,需要的朋友可以参考下
    2014-02-02
  • Android视图的绘制流程(上) View的测量

    Android视图的绘制流程(上) View的测量

    这篇文章主要为大家详细介绍了Android视图的绘制流程上篇,View测量的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-01-01
  • android gradle如何修改生成的apk名字

    android gradle如何修改生成的apk名字

    Gradle是当前非常“劲爆”得构建工具,而这篇文章主要给大家介绍了关于android gradle如何修改生成的apk名字的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。
    2017-12-12
  • 如何利用Kotlin实现极简回调

    如何利用Kotlin实现极简回调

    这篇文章主要给大家介绍了关于如何利用Kotlin实现极简回调的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-01-01
  • Android JNI c/c++调用java的实例

    Android JNI c/c++调用java的实例

    这篇文章主要介绍了Android JNI c/c++调用java的实例的相关资料,需要的朋友可以参考下
    2017-07-07
  • Android中Textview和图片同行显示(文字超出用省略号,图片自动靠右边)

    Android中Textview和图片同行显示(文字超出用省略号,图片自动靠右边)

    Android中Textview和图片同行显示,文字超出用省略号显示,图片自动靠右边。看到这个问题本来认为是一个很正常的需求,看起来很简单,但是做起来却遇到了很蛋疼的问题,怎么搞的都不行,堵了很长时间,下面说一下解决的方案,希望遇到这样问题的朋友可以使用。
    2016-12-12
  • Android项目迁移到AndroidX的方法步骤

    Android项目迁移到AndroidX的方法步骤

    这篇文章主要介绍了Android项目迁移到AndroidX的方法步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-12-12

最新评论