網站首頁 編程語言 正文
簡介
Android Studio里在4.0前有一種ProgressDialog,這個已經淘汰了。我們完全可以使用ProgressBar來取代。但是還有一種Dialog叫PopWindow,它是一種“可阻塞式Dialog”。即彈出后除非你給它一個“動作”否則就一直顯示在那。
今天我們就來看看這種Dialog的使用,如下面界面截圖。
PopWindow里是可以嵌套一個View的,這個View就好比我們之前學習的Adapter模式。你只要繪制好一個自己布局的View,然后使用:LayoutInflater.from(ctx).inflate(R.layout.item_popup, null, false);加載你的自定義View并使用PopupWindow popWindow = new PopupWindow(自定義View......)即可完成“嵌套”。
下面我們就來看實際例子。
課程目標
- 屏幕中間有一個【彈出Windows】的按鈕
- 點擊后該彈出內嵌2個子菜單,一個【湯姆】一個【杰瑞】;
- 在空白處點擊屏幕,該彈出的窗口消失;
- 點擊【湯姆】顯示Toast;
- 點擊【杰瑞】顯示Toast并關閉彈出窗口;
前端代碼
item_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:orientation="vertical"> <Button android:id="@+id/btnTom" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="5dp" android:text="湯姆" android:textSize="18sp" /> <Button android:id="@+id/btnJerry" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="5dp" android:text="杰瑞" android:textSize="18sp" /> </LinearLayout>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <Button android:layout_centerInParent="true" android:id="@+id/btnPopWindow" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="彈出Window" /> </RelativeLayout>
后端代碼
MainActivity.java
package org.mk.android.demopopupdialog;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private Button btnPopWindow;
private Context ctx;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ctx=MainActivity.this;
btnPopWindow=(Button)findViewById(R.id.btnPopWindow);
btnPopWindow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
popWindow(view);
}
});
}
private void popWindow(View v) {
View subDlgView = LayoutInflater.from(ctx).inflate(R.layout.item_popup,
null, false);
Button btnTom = (Button) subDlgView.findViewById(R.id.btnTom);
Button btnJerry = (Button) subDlgView.findViewById(R.id.btnJerry);
//1.構造一個PopupWindow,參數依次是加載的View,寬高
final PopupWindow popWindow = new PopupWindow(subDlgView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
//這些為了點擊非PopupWindow區域,PopupWindow會消失的,如果沒有下面的
//代碼的話,你會發現,當你把PopupWindow顯示出來了,無論你按多少次后退鍵
//PopupWindow并不會關閉,而且退不出程序,加上下述代碼可以解決這個問題
popWindow.setTouchable(true);
popWindow.setTouchInterceptor(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return false;
// 這里如果返回true的話,touch事件將被攔截
// 攔截后 PopupWindow的onTouchEvent不被調用,這樣點擊外部區域無法dismiss
}
});
popWindow.setBackgroundDrawable(new ColorDrawable(0x00000000)); //要為popWindow設置一個背景才有效
//設置popupWindow顯示的位置,參數依次是參照View,x軸的偏移量,y軸的偏移量
popWindow.showAsDropDown(v, 50, 0);
//設置popupWindow里的按鈕的事件
btnTom.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "你點擊了Tom, Tom吃Jerry", Toast.LENGTH_SHORT).show();
}
});
btnJerry.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "你點擊了Jerry, Jerry揍Tom", Toast.LENGTH_SHORT).show();
popWindow.dismiss();
}
});
}
}
原文鏈接:https://blog.csdn.net/lifetragedy/article/details/128028734
相關推薦
- 2022-06-08 ASP.NET?Core中MVC模式實現路由二_實用技巧
- 2022-06-22 深入淺析C/C++?的條件編譯_C 語言
- 2022-06-21 C語言平衡二叉樹真題練習_C 語言
- 2022-11-05 React+CSS?實現繪制橫向柱狀圖_React
- 2022-09-29 Python執行時間計算方法以及優化總結_python
- 2022-09-13 Go語言中map使用和并發安全詳解_Golang
- 2022-02-24 解決:this is incompatible with sql_mode=only_full_gr
- 2023-07-13 react中useState的基本用法
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支