- 浏览: 136158 次
- 性别:
- 来自: 广州
文章分类
- 全部博客 (172)
- java (172)
- c# winform 关于窗体最大化时的是否全屏效果与是否遮盖任务栏 (1)
- c# winform 只运行一个程序实例 (1)
- Shiro权限框架 (1)
- Java字节码(.class文件)的代码解析 (1)
- Hibernate、Spring和Struts工作原理及使用理由 (1)
- 基本PKG处理命令 (1)
- 最近写的代码的小结 (1)
- Dual Palindromes (1)
- 编译TortoiseSVN1.7源代码笔记 (1)
- Android项目代码混淆 (1)
- 关于就业叨咕几句 (1)
- Java重定向System.out和System.err (1)
- IHS与WAS集成插件静默安装 (1)
- 专题地图概述 (1)
- 关于MapXtreme2004附带Sample不能运行的问题 (1)
- SQL注入漏洞 (1)
- 回顾过去 展望未来(写给自己) (1)
- DB2认证考试经验谈(700&&701) (1)
- 今天用到了RM格式文件的分割工具RealProducer (1)
- 搜索引擎还是很有发展前途的 (1)
- 泰利德破解,实现学校机房免费上网 (1)
- Arduino 引言:移动互联外设传感展望 (1)
- c/c++ static 用法总结(三版本合一) (1)
- JQuery与xml的组合谈 (1)
- Android开发之消息处理机制(一)——Handler (1)
- Android开发之PopupWindow (1)
- 解析BitmapData.getPixel32()返回值因何不准确 (1)
- 关于Ext引用js的顺序问题 (1)
- Js事件大全 (1)
- 使用JavaFX2.0编写国际象棋游戏 (1)
- Hibernate/JPA常见异常分析与解决 (1)
最新评论
-
amcucn:
这排版看得
Shiro权限框架 -
WAMING5:
这也太紧凑了,眼看花了
Shiro权限框架 -
longzhun:
大虾 ,看得出你很懒啊!发个贴 这样子怎么看!!!
Shiro权限框架 -
swanky_yao:
活干的不细!!
Spring+Hibernate框架下Mysql读写分离、主从数据库配 -
饶首建:
不错,不过用处比较少吧
批处理设ip和dns
<h1>Android开发之PopupWindow</h1>
<p>/*</p>
<p>* Android开发之PopupWindow</p>
<p>*</p>
<p>* Created on: 2011-8-8</p>
<p>* Author: blueeagle</p>
<p>* Email: liujiaxiang@gmail.com</p>
<p>*/</p>
<p> 聪明的人善于总结,记录,不知道这是谁说的了,反正要当一个聪明人,我得先学会总结,记录。最近在Android的学习过程中,发现PopupWindow也是值得研究一番的一个东东,因此拿过来说道说道。与其相似的就归纳到一起说道吧,那就是AlertDialog和Toast。</p>
<h2>PopupWindow</h2>
<p>java.lang.Object </p>
<p> ? android.widget.PopupWindow</p>
<p>手册中,对PopupWindow的描述是这样的:它是一个弹出的窗口,可以用来显示一个任意的视图。弹出窗口是当前活动上出现一个浮动容器。</p>
<p> PopupWindow的性质,其实就是我们通常所说的“模态对话框”。只有在其退出之后,才可以进行外部线程的操作。下面对其进行详细说明。</p>
<h3>简单的PopupWindow</h3>
<p><img alt="" src="http://hi.csdn.net/attachment/201108/9/0_1312883088dqj5.gif"></p>
<p><img alt="" src="http://hi.csdn.net/attachment/201108/9/0_1312883108uFWu.gif"></p>
<p> 如图所示,通过主Activity中的一个按钮来触发弹出窗口操作。这个窗口什么都不干,就是显示一句话:“狂热的Android开发者”。为了增加可见性,我设置了不同的背景色加以区分。</p>
<p> 源码如下:</p>
<textarea readonly name="code" class="java">/*
* Android开发之PopupWindow、AlertDialog和Toast
* PopWinEx.java
* Created on: 2011-8-8
* Author: blueeagle
* Email: liujiaxiang@gmail.com
*/
package com.blueeagle;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.PopupWindow;
public class PopWinEx extends Activity {
/** Called when the activity is first created. */
Button MyButton;
PopupWindow pw;
View myView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MyButton = (Button)findViewById(R.id.myButton);
MyButton.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
myView = getLayoutInflater().inflate(R.layout.pop,null);
pw = new PopupWindow(myView,500,200,true);
pw.showAsDropDown(MyButton);
}
});
}
}
</textarea><p><br></p>
<p>我们给PopupWindow和主界面,分别定义一个layout。对应的XML文件如下:</p>
<textarea readonly name="code" class="html">pop.xml
<?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"
android:background="#ffff00"
>
<TextView
android:id="@+id/mytextview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>
main.xml
<?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"
android:background="#ff00ff"
>
<Button
android:id="@+id/myButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>
</textarea><p><br></p>
<p><strong>说明:这里值得注意的是,我们要给弹出窗口设置焦点,</strong>pw = <strong>new</strong> PopupWindow(myView,500,200,<strong>true</strong>);<strong>这句中的true表示弹出窗口可以获得焦点。如果弹出窗口没有获得焦点的时候,不断点击按钮的,最终程序将退出,这时是因为内存耗尽的原因,可以通过查看log得到。</strong></p>
<p><strong></strong></p>
<h3>有一定布局的PopupWindow</h3>
<p><img alt="" src="http://hi.csdn.net/attachment/201108/9/0_1312883128M11K.gif"></p>
<p><img alt="" src="http://hi.csdn.net/attachment/201108/9/0_1312883208azyM.gif"></p>
<p><img alt="" src="http://hi.csdn.net/attachment/201108/9/0_1312883226t3zX.gif"></p>
<p>如图所示,同样是通过主Activity中的一个按钮来触发弹出窗口操作。但是现在这个弹出窗口可以进行一些操作,因为其具有了一定的布局,目前我们暂且其可以进行的操作为:</p>
<p>1. 可编辑功能</p>
<p>2. 可传递信息</p>
<p>3. 弹出窗口上再次弹出窗口</p>
<p>4. 可以取消当前弹出窗口</p>
<p>为了增加可见性,我依然设置了不同的背景色加以区分。</p>
<p> 源码如下:</p>
<textarea readonly name="code" class="java">/*
* Android开发之PopupWindow、AlertDialog和Toast
* PopWinEx.java
* Created on: 2011-8-9
* Author: blueeagle
* Email: liujiaxiang@gmail.com
*/
package com.blueeagle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
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;
import android.widget.TextView;
public class PopWinEx extends Activity {
/** Called when the activity is first created. */
Button MyButton;
Button MyExit;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MyButton = (Button)findViewById(R.id.myButton);
MyExit = (Button)findViewById(R.id.myExit);
MyButton.setOnClickListener(new clickEvent());
MyExit.setOnClickListener(new ExitEvent());
}
class clickEvent implements OnClickListener {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v==MyButton) {
showPopWindow(PopWinEx.this,MyButton);
}
}
}
class ExitEvent implements OnClickListener {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v==MyExit) {
finish();
}
}
}
private void showPopWindow(Context context, View parent) {
// TODO Auto-generated method stub
final PopupWindow pw;
View myView;
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
myView = inflater.inflate(R.layout.pop, null);
Button pop_OK = (Button)myView.findViewById(R.id.button_ok);
Button pop_Cancel = (Button)myView.findViewById(R.id.button_cancel);
final EditText pop_User = (EditText)myView.findViewById(R.id.edittext);
final EditText pop_Password = (EditText)myView.findViewById(R.id.password);
pw = new PopupWindow(myView,500,200,true);
pop_OK.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
showPop_PopWindow();
// TODO Auto-generated method stub
}
private void showPop_PopWindow() {
// TODO Auto-generated method stub
View myView1;
myView1 = getLayoutInflater().inflate(R.layout.pop1,null);
TextView User_Is = (TextView)myView1.findViewById(R.id.useris);
TextView Password_Is = (TextView)myView1.findViewById(R.id.passwordis);
User_Is.setText(pop_User.getText().toString());
Password_Is.setText(pop_Password.getText().toString());
final PopupWindow pw_pw;
pw_pw = new PopupWindow(myView1,500,200,true);
Button pop_pop_OK = (Button)myView1.findViewById(R.id.button_ok1);
Button pop_pop_Cancel = (Button)myView1.findViewById(R.id.button_cancel1);
pop_pop_Cancel.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
pw_pw.dismiss();//
}
});
pop_pop_OK.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
AlertDialog.Builder my_ADialog =new AlertDialog.Builder(PopWinEx.this);
my_ADialog.setTitle("我是弹出对话框的弹出对话框");
my_ADialog.setMessage("怎么样?学会了吗?");
my_ADialog.show();
}
});
pw_pw.showAtLocation(myView1.findViewById(R.id.button_ok1), Gravity.CENTER, 200, 200);
}
});
pop_Cancel.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
pw.dismiss();//
}
});
pw.showAsDropDown(MyButton);
}
}
</textarea><p><br></p>
<p>我们给PopupWindow,弹出窗口的弹出窗口和主界面,分别定义一个layout。对应的XML文件如下:</p>
<textarea readonly name="code" class="html">main.xml
<?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"
android:background="#ff00ff"
>
<Button
android:id="@+id/myButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/myExit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="退出程序"
/>
</LinearLayout>
pop1.xml
<?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"
android:background="#ffff00"
>
<TextView
android:id="@+id/mytextview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<TextView
android:text="您输入的账号是:"
android:layout_width="180dip"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/useris"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:text="您输入的密码是:"
android:layout_width="180dip"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/passwordis"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffff00"
>
<Button
android:id="@+id/button_ok1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="确定"
/>
<Button
android:id="@+id/button_cancel1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="取消"
/>
</LinearLayout>
</LinearLayout>
pop.xml
<?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"
android:background="#ffff00"
>
<TextView
android:id="@+id/mytextview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<TextView
android:id="@+id/mytextview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="账号"
/>
<EditText
android:id="@+id/edittext"
android:layout_width="180dip"
android:layout_height="wrap_content"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码"
/>
<EditText
android:id="@+id/password"
android:layout_width="180dip"
android:layout_height="wrap_content"
android:password="true"
/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffff00"
>
<Button
android:id="@+id/button_ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="确定"
/>
<Button
android:id="@+id/button_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="取消"
/>
</LinearLayout>
</LinearLayout>
</textarea><p align="left"><br></p>
<p align="left"><strong>说明:这里值得注意的是,弹出窗口上再次弹出窗口需要将调用</strong>findViewById<strong>函数的视图明确。然后将相应的变量设置成final类型。弹出窗口其实是一个View,这个View需要用</strong> LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.<em>LAYOUT_INFLATER_SERVICE</em>);</p>
<p> myView = inflater.inflate(R.layout.<em>pop</em>, <strong>null</strong>);<strong>来进行索引。找到相应的xml布局文件,来安排弹出窗口是什么样的。当然,在实际开发中,可能会遇见没有xml布局文件的View,这怎么办呢?直接new出来就可以了,比如:</strong></p>
<p align="left"> mView = <strong>new</strong> PopView(mContext, 800, 400);</p>
<p> PopupWindow pw= <strong>new</strong> PopupWindow(mView,800,400,<strong>true</strong>);</p>
<p><strong>这里我没有对string做特别的处理,因为时间比较紧张。这并不是一个好的编程习惯。希望大家把语言类的东西都放在string里去,不要硬写在程序代码里。</strong></p>
<h3>PopupWindow的特殊效果</h3>
<p><img alt="" src="http://hi.csdn.net/attachment/201108/9/0_1312883254K44X.gif"></p>
<p>PopupWindow的效果可以做的很炫,可以有边框,圆角,透明,渐变色,动画等。下面逐一来实现。</p>
<p>比如我要在上面的例子中实现这些操作,即可添加一个语句:</p>
<p> myView.setBackgroundResource(R.drawable.<em>round_win</em>);</p>
<p>当然,最主要的就是round_win.xml里面所写的内容了:</p>
<textarea readonly name="code" class="html">round_win.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient android:startColor="#e0000fff" android:endColor="#e000ff00"
android:angle="90" /><!--背景颜色渐变 -->
<stroke android:dashWidth="2dp" android:dashGap="2dp"
android:width="2dp" android:color="#ff0000"></stroke>
<!--描边 -->
<corners android:bottomRightRadius="5dp"
android:bottomLeftRadius="5dp" android:topLeftRadius="5dp"
android:topRightRadius="5dp" /><!--设置圆角-->
</shape>
</textarea><p><br></p>
<p>当然,最主要的就是round_win.xml里面所写的内容了。对于上面,这条 shape 的定义,分别为渐变,在gradient 中startColor属性为开始的颜色,endColor 为渐变结束的颜色,下面的 angle 是角度。接下来是 stroke可以理解为边缘,dashWidth 表示宽度,dashGap 表示断续;corners 为拐角这里radius 属性为半径,最后是相对位置属性 padding。</p>
<p>android:color 是一个16 进制颜色。这个颜色由RGB 值指定,可带Alpha 。必须以“# ”开头,后面跟随Alpha-Red-Green-Blue。Alpha表示的是透明度,0为全透明,ff为全不透明。</p>
<p> 以上完成了弹出窗口的边框,渐变色,透明,圆角等操作。</p>
<p>下面进行有意思的动画操作。</p>
<p> 先设置一下弹出窗口的进入和退出的xml文件。首先要在res目录下建立文件夹anim,里面建立动画描述文件。如下所示:</p>
<textarea readonly name="code" class="html">ani_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:fromYDelta="-100"
android:toYDelta="0"
android:duration="1000"
android:fillEnabled="true"
android:fillAfter="true"
/>
<scale android:fromXScale="0.6" android:toXScale="1.0"
android:fromYScale="0.6" android:toYScale="1.0" android:pivotX="50%"
android:pivotY="50%" android:duration="2000" />
<alpha android:interpolator="@android:anim/decelerate_interpolator"
android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="2000" />
</set>
ani_out.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="true"
>
<translate android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:fromYDelta="0"
android:toYDelta="-100"
android:duration="1000"
android:fillEnabled="true"
android:fillAfter="true"
/>
<scale android:fromXScale="1.0" android:toXScale="0.4"
android:fromYScale="1.0" android:toYScale="0.4" android:pivotX="50%"
android:pivotY="50%" android:duration="2000" />
<alpha android:interpolator="@android:anim/decelerate_interpolator"
android:fromAlpha="1.0"
android:toAlpha="0.0"
android:duration="2000"
/>
</set>
</textarea><p><br></p>
<p>在Values文件夹中建立style.xml文件如下:</p>
<p><strong><em>style.xml</em></strong></p>
<p></p>
<textarea readonly name="code" class="html"><?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="mytheme" parent="@android:style/Theme.Light">
<item name="android:windowNoTitle"> true </item>
</style>
<style name="PopupAnimation" parent="android:Animation">
<item name="android:windowEnterAnimation">@anim/ani_in</item>
<item name="android:windowExitAnimation">@anim/ani_out</item>
</style>
</resources>
</textarea><p><br></p>
<p> 最后在Activity代码文件中加入:</p>
<p> pw.setAnimationStyle(R.style.<em>PopupAnimation</em>);</p>
<p>动画效果就出来了,怎么样?很炫吧。</p>
发表评论
-
Hibernate/JPA常见异常分析与解决
2012-02-07 17:24 14621. ClassNotFoundException: ... -
使用JavaFX2.0编写国际象棋游戏
2012-02-07 16:24 1054前面发布了不少Javafx2的教程,不觉手痒,想尝试一 ... -
Js事件大全
2012-02-04 15:34 743一般事件 事件 ... -
关于Ext引用js的顺序问题
2012-02-02 15:49 716今天自己搞了个ext的helloworld,所需的包都 ... -
解析BitmapData.getPixel32()返回值因何不准确
2012-02-02 12:29 948<div style="widt ... -
Android开发之消息处理机制(一)——Handler
2012-01-11 15:19 1127<h1>Android开发之消息处理机制( ... -
JQuery与xml的组合谈
2011-12-21 17:18 766JQuery与xml的组合谈 今天谈的是XML,学ja ... -
c/c++ static 用法总结(三版本合一)
2011-12-21 10:34 778<span style="&qu ... -
Arduino 引言:移动互联外设传感展望
2011-12-20 14:08 1288作者:mznewfacer (Wolf Geek) ... -
泰利德破解,实现学校机房免费上网
2011-12-20 13:58 915泰利德破解,实现学校机房免费上网<br> ... -
搜索引擎还是很有发展前途的
2011-12-19 11:49 919相信大家已经听说,在头几天,搜狐推出了一个专业搜索门户 ... -
今天用到了RM格式文件的分割工具RealProducer
2011-12-19 09:19 663项目要加一些视频文件,这些视频都是几个老师对数据结构课 ... -
DB2认证考试经验谈(700&&701)
2011-12-16 17:17 1075很多人都通过了D ... -
回顾过去 展望未来(写给自己)
2011-12-15 16:19 1443本来想用“昨天今天明天”做标题来着,但是人家本山大叔说 ... -
SQL注入漏洞
2011-12-15 12:19 687CSDN上的ASP.NET电子杂志下载下来看,就看到了 ... -
关于MapXtreme2004附带Sample不能运行的问题
2011-12-15 10:54 642</span> -
专题地图概述
2011-12-14 12:49 797<p class="MsoNorma ... -
IHS与WAS集成插件静默安装
2011-12-14 12:04 930<span style="col ... -
Java重定向System.out和System.err
2011-12-13 14:49 1137<div>继承PrintStream类: ... -
关于就业叨咕几句
2011-12-13 11:04 605公司开始去各个学校开宣讲会了,这让我想起又到招聘的季节 ...
相关推荐
Android开发之PopupWindow实现弹窗
本文实例讲述了Android开发之PopupWindow创建弹窗、对话框的方法。分享给大家供大家参考,具体如下: 简介: PopupWindow 可创建类似对话框风格的窗口 效果: 使用方法: 使用PopupWindow 创建对话框风格的串口秩序...
Android TV 开发有关PopupWindow的KeyListener
安卓开发-Android之用PopupWindow实现弹出菜单.zip
android开发PopupWindow实现跟随试弹出框
今天小编就为大家分享一篇关于Android开发解决popupWindow重叠报错问题的文章,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
Android PopupWindow使用示例,更多信息请访问http://blog.csdn.net/qingdujun/article/details/42609549
仿微信右上角弹出PopupWindow的Demo,
Android开发中相信下图所示界面大家都不陌生,该种弹出框的使用频率也是极高的,所以我专门谢了个类用于方便的弹出该界面。并把确定或取消后的逻辑通过抽象方法的方式让用户自己实现,大大提高了开发效率。下面是该...
在android 开发中,让popupwindow 弹出在点击按钮的上方中间; 说明文档见我的博客
本文实例讲述了Android开发实现popupWindow弹出窗口自定义布局与位置控制方法。分享给大家供大家参考,具体如下: 布局文件: 主布局文件:activity_main: <?xml version=1.0 encoding=utf-8?> <...
主要介绍了Android中的popupwindow进入和退出的动画,需要的朋友可以参考下
主要介绍了Android 开发使用PopupWindow实现加载等待界面功能,结合实例形式分析了Android使用PopupWindow组件实现加载等待界面功能相关布局与功能实现技巧,需要的朋友可以参考下
主要介绍了Android开发中PopupWindow用法,结合实例形式分析了PopupWindow弹出窗口效果的使用技巧,需要的朋友可以参考下
Android 应用项目开发源码 参考和学习使用