服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - Android 开发使用PopupWindow实现加载等待界面功能示例

Android 开发使用PopupWindow实现加载等待界面功能示例

2022-12-16 15:03LIFE_R Android

这篇文章主要介绍了Android 开发使用PopupWindow实现加载等待界面功能,结合实例形式分析了Android使用PopupWindow组件实现加载等待界面功能相关布局与功能实现技巧,需要的朋友可以参考下

本文实例讲述了Android 开发使用PopupWindow实现加载等待界面功能。分享给大家供大家参考,具体如下:

实现加载等待界面我用了两种方式,一种是用PopupWindow实现,另一种便是用Activity实现。用Activity实现方法请见我的另一篇博客:

Android 使用Activity实现加载等待界面

首先看效果:

Android 开发使用PopupWindow实现加载等待界面功能示例

用PopupWindow实现此功能还是比较简单的,首先我们写一个布局,只有一个登录按钮,用于触发等待界面:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:id="@+id/activity_main"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 android:orientation="vertical"
 tools:context="com.toprs.myapplication.MainActivity">
 
 <Button
  android:text="登录"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:onClick="loginClick"
  android:id="@+id/button2"/>
</LinearLayout>

然后为登录按钮添加监听事件:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package com.wang.myapplication;
 
import ...
 
public class MainActivity extends AppCompatActivity {
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
 }
 
 public void loginClick(View v){
  final PopupWindow popupWindow = new PopupWindow();
  popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
  popupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
  popupWindow.setFocusable(true);
  View view = LayoutInflater.from(this).inflate(R.layout.popup,null);
  popupWindow.setContentView(view);
  popupWindow.showAtLocation(getWindow().getDecorView(), Gravity.CENTER,0,0);
 
  new Handler().postDelayed(new Runnable() {
   @Override
   public void run() {
    Toast.makeText(MainActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
    popupWindow.dismiss();
   }
  },2000);
 }
}

其中弹出的PopupWindow需要一个布局,也就是简单放入一个ProgressBar:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="200dp"
    android:layout_height="200dp">
 
 <ProgressBar
  android:id="@+id/progressBar4"
  style="?android:attr/progressBarStyle"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_centerInParent="true"/>
 
</RelativeLayout>

大功告成,运行一下即可!!

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

原文链接:https://blog.csdn.net/wangtaocsdn/article/details/70983877

延伸 · 阅读

精彩推荐