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

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

服务器之家 - 编程语言 - Android - Android入门之PopupWindow用法实例解析

Android入门之PopupWindow用法实例解析

2021-03-06 23:00Android开发网 Android

这篇文章主要介绍了Android入门之PopupWindow用法,对于Android初学者来说有一定的学习借鉴价值,需要的朋友可以参考下

本文实例介绍一下popupwindow对话框。popupwindow是阻塞对话框,只有在外部线程 或者 popupwindow本身做退出操作才可以执行。popupwindow完全依赖layout做外观,在常见的开发中,popupwindow应该会与alertdialog常混用。

先贴出本例中运行的结果图:

Android入门之PopupWindow用法实例解析

main.xml的源码如下:

?
1
2
3
4
5
6
7
8
<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  >
<button android:id="@+id/button01" android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="popupwindow演示"></button>
</linearlayout>

下图所示是popupwindow弹出的截图,这里的popupwindow是个登录框,点“确定”则自动填写,点“取消”则关闭popupwindow。

 Android入门之PopupWindow用法实例解析

popupwindow.xml的源码如下:

?
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
31
32
33
<?xml version="1.0" encoding="utf-8"?>
 
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent" android:layout_height="wrap_content"
 android:orientation="vertical" android:background="#000000">
 
 <textview android:id="@+id/username_view"
 android:layout_height="wrap_content"
 android:layout_marginleft="20dip"
 android:layout_marginright="20dip" android:text="用户名"
 android:textappearance="?android:attr/textappearancemedium" android:layout_width="fill_parent"/>
 
 <edittext android:id="@+id/username_edit"
 android:layout_height="wrap_content"
 android:layout_width="fill_parent" android:layout_marginleft="20dip"
 android:layout_marginright="20dip" android:capitalize="none"
 android:textappearance="?android:attr/textappearancemedium" />
 
 <textview android:id="@+id/password_view"
 android:layout_height="wrap_content"
 android:layout_marginleft="20dip"
 android:layout_marginright="20dip" android:text="密码"
 android:textappearance="?android:attr/textappearancemedium" android:layout_width="fill_parent"/>
 
 <edittext android:id="@+id/password_edit"
 android:layout_height="wrap_content"
 android:layout_width="fill_parent" android:layout_marginleft="20dip"
 android:layout_marginright="20dip" android:capitalize="none"
 android:password="true"
 android:textappearance="?android:attr/textappearancemedium" />
 
<linearlayout android:id="@+id/linearlayout01" android:layout_height="wrap_content" android:layout_width="fill_parent" android:gravity="center"><button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btnok" android:layout_weight="100" android:text="确定"></button><button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="100" android:text="取消" android:id="@+id/btncancel"></button></linearlayout>
</linearlayout>

接下来是java程序源码:

?
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package com.testalertdialog;
 
import android.app.activity;
import android.app.alertdialog;
import android.content.context;
import android.content.dialoginterface;
import android.os.bundle;
import android.text.editable;
import android.view.gravity;
import android.view.layoutinflater;
import android.view.view;
import android.view.view.onclicklistener;
import android.widget.button;
import android.widget.edittext;
import android.widget.popupwindow;
 
 
public class testalertdialog extends activity {
 button btnpopupwindow;
  /** called when the activity is first created. */
  @override
  public void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.main);
    //定义按钮
    btnpopupwindow=(button)this.findviewbyid(r.id.button01);
    btnpopupwindow.setonclicklistener(new clickevent());
  }
  
  
  //统一处理按键事件
  class clickevent implements onclicklistener{
 
   @override
   public void onclick(view v) {
   // todo auto-generated method stub
   if(v==btnpopupwindow)
   {
    showpopupwindow(testalertdialog.this,
     testalertdialog.this.findviewbyid(r.id.button01));
   }
   }
  }
 
 public void showpopupwindow(context context,view parent){
 layoutinflater inflater = (layoutinflater) 
    context.getsystemservice(context.layout_inflater_service); 
   final view vpopupwindow=inflater.inflate(r.layout.popupwindow, null, false);
   final popupwindow pw= new popupwindow(vpopupwindow,300,300,true);
 
   //ok按钮及其处理事件
    button btnok=(button)vpopupwindow.findviewbyid(r.id.btnok);
    btnok.setonclicklistener(new onclicklistener(){
  @override
  public void onclick(view v) {
  //设置文本框内容
    edittext edtusername=(edittext)vpopupwindow.findviewbyid(r.id.username_edit);
    edtusername.settext("username");
    edittext edtpassword=(edittext)vpopupwindow.findviewbyid(r.id.password_edit);
    edtpassword.settext("password");
  }
    });
    
   //cancel按钮及其处理事件
    button btncancel=(button)vpopupwindow.findviewbyid(r.id.btncancel);
    btncancel.setonclicklistener(new onclicklistener(){
  @override
  public void onclick(view v) {
  pw.dismiss();//关闭
  }
    });
    //显示popupwindow对话框
   pw.showatlocation(parent, gravity.center, 0, 0);
 }
}

 

延伸 · 阅读

精彩推荐