Android开发如何使用PopupWindow实现弹出警告框

发布时间:2020-07-23 09:22:28 作者:小猪
来源:亿速云 阅读:169

这篇文章主要为大家展示了Android开发如何使用PopupWindow实现弹出警告框,内容简而易懂,希望大家可以学习一下,学习完之后肯定会有收获的,下面让小编带大家一起来看看吧。

Android开发中相信下图所示界面大家都不陌生,该种弹出框的使用频率也是极高的,所以我专门谢了个类用于方便的弹出该界面。并把确定或取消后的逻辑通过抽象方法的方式让用户自己实现,大大提高了开发效率。下面是该类:

Android开发如何使用PopupWindow实现弹出警告框

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代码如下:

<&#63;xml version="1.0" encoding="utf-8"&#63;>
<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();
      }
    });
  }
}

即如下效果:

Android开发如何使用PopupWindow实现弹出警告框

So,以后使用只需要简单调用几句代码就好了!

以上就是关于Android开发如何使用PopupWindow实现弹出警告框的内容,如果你们有学习到知识或者技能,可以把它分享出去让更多的人看到。

推荐阅读:
  1. 使用Bootstrap弹出框和警告框插件详解
  2. 关于使用PopupWindow

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

android popupwindow roi

上一篇:使用PHPCMS用哪个数据库比较好

下一篇:cisco路由器的启动过程及密码重置

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》