欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android 開發(fā)使用PopupWindow實現(xiàn)彈出警告框的復用類示例

 更新時間:2020年05月21日 10:31:10   作者:LIFE_R  
這篇文章主要介紹了Android 開發(fā)使用PopupWindow實現(xiàn)彈出警告框的復用類,結合實例形式分析了Android基于PopupWindow彈出警告框的復用類具體布局與功能實現(xiàn)技巧,需要的朋友可以參考下

本文實例講述了Android 開發(fā)使用PopupWindow實現(xiàn)彈出警告框的復用類。分享給大家供大家參考,具體如下:

Android開發(fā)中相信下圖所示界面大家都不陌生,該種彈出框的使用頻率也是極高的,所以我專門謝了個類用于方便的彈出該界面。并把確定或取消后的邏輯通過抽象方法的方式讓用戶自己實現(xiàn),大大提高了開發(fā)效率。下面是該類:

package com.***.popupwindow;

import ******;

public abstract class MyPopupWindow {

  private PopupWindow popupWindow;
  private Activity context;
  private String content;
  private String positiveWord = "確定";
  private String negativeWord = "取消";

  /**
   * 構造函數(shù)
   *
   * @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();
      }
    });
  }

  /**
   * 確定鍵按下后執(zhí)行
   */
  public abstract void sureClick();

  /**
   * 取消鍵按下后執(zhí)行
   */
  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開發(fā)入門與進階教程》、《Android視圖View技巧總結》、《Android編程之activity操作技巧總結》、《Android數(shù)據(jù)庫操作技巧總結》及《Android資源操作技巧匯總

希望本文所述對大家Android程序設計有所幫助。

相關文章

  • Android之自定義實現(xiàn)BaseAdapter(通用適配器二)

    Android之自定義實現(xiàn)BaseAdapter(通用適配器二)

    這篇文章主要為大家詳細介紹了Android之自定義實現(xiàn)BaseAdapter通用適配器第二篇,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-08-08
  • Android開發(fā)中的MVC設計模式淺析

    Android開發(fā)中的MVC設計模式淺析

    這篇文章主要介紹了Android開發(fā)中的MVC設計模式淺析,本文講解了對Android開發(fā)中的MVC設計模式的理解,需要的朋友可以參考下
    2015-06-06
  • Android文本視圖TextView實現(xiàn)跑馬燈效果

    Android文本視圖TextView實現(xiàn)跑馬燈效果

    這篇文章主要為大家詳細介紹了Android文本視圖TextView實現(xiàn)跑馬燈效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-05-05
  • Android EditText搜索框實現(xiàn)圖標居中

    Android EditText搜索框實現(xiàn)圖標居中

    本篇文章主要介紹了Android EditText搜索框實現(xiàn)圖標居中,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-07-07
  • EasyValidate優(yōu)雅地校驗提交數(shù)據(jù)完整性

    EasyValidate優(yōu)雅地校驗提交數(shù)據(jù)完整性

    這篇文章主要介紹了EasyValidate優(yōu)雅地校驗提交數(shù)據(jù)完整性,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-12-12
  • Android仿QQ好友列表分組實現(xiàn)增刪改及持久化

    Android仿QQ好友列表分組實現(xiàn)增刪改及持久化

    這篇文章主要介紹了Android仿QQ好友列表分組實現(xiàn)增刪改及持久化的相關資料,需要的朋友可以參考下
    2016-01-01
  • Android關鍵字persistent詳細分析

    Android關鍵字persistent詳細分析

    這篇文章主要介紹了Android關鍵字persistent的相關資料,幫助大家更好的理解和學習使用Android,感興趣的朋友可以了解下
    2021-04-04
  • Android實現(xiàn)刮刮樂示例分析

    Android實現(xiàn)刮刮樂示例分析

    本文實現(xiàn)了Android刮刮樂示例分析,刮獎在生活中常常見到,網(wǎng)上現(xiàn)在也有各種各樣的抽獎活動,下面我們就要實現(xiàn)一個刮刮樂程序。
    2016-10-10
  • Android中GPS坐標轉換為高德地圖坐標詳解

    Android中GPS坐標轉換為高德地圖坐標詳解

    最近因為公司需求,在做GPS定位,并且將獲得的坐標顯示在高德地圖上,但是實際效果跟我們期望的是有偏差的。通過查閱資料,才知道有地球坐標、火星坐標之說。下面這篇文章就詳細介紹了Android中GPS坐標轉換為高德地圖坐標的方法,需要的朋友可以參考下。
    2017-01-01
  • Android 自定義View之倒計時實例代碼

    Android 自定義View之倒計時實例代碼

    這篇文章主要介紹了Android 自定義View之倒計時實例代碼的相關資料,大多數(shù)app在注冊的時候,都有一個獲取驗證碼的按鈕,點擊后,訪問接口,最終用戶會收到短信驗證碼。為了不多次寫這個獲取驗證碼的接口,下面將它自定義成一個view,方便使用,需要的朋友可以參考下
    2017-04-04

最新評論