Android PopupWindow的使用

本文通过实战案例介绍了 Android 开发中 PopupWindow 的使用方法。包括如何通过按钮点击事件触发 PopupWindow 显示,以及如何利用定时器和 Handler 实现 PopupWindow 的自动弹出。

 

这一节讲的是Android PopupWindow的使用! 在我理解其实PopupWindow其实类似于一个不能动的Widget(仅从显示效果来说!)它是浮在别的窗口之上的.下面我将给大家做一个简单的Demo,类似于音乐播放器的Widget的效果,点击Button的时候出来PopupWindow,首先我们看一下效果图:

0_127350313844hZ.gif

 

 

0_1273503154Vj4U.gif

下载 (10.47 KB)
2010-7-27 17:34

 

 

下面是核心代码:

package com.android.tutor; 
import android.app.Activity; 
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.view.ViewGroup.LayoutParams; 
import android.widget.Button; 
import android.widget.PopupWindow; 
public class PopupWindowDemo extends Activity  implements OnClickListener{ 
    private Button btn; 
     
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
         
        btn = (Button)findViewById(R.id.btn); 
        btn.setOnClickListener(this); 
    } 
    @Override 
    public void onClick(View v) { 
        Context mContext = PopupWindowDemo.this; 
        if (v.getId() == R.id.btn) { 
            LayoutInflater mLayoutInflater = (LayoutInflater) mContext 
                    .getSystemService(LAYOUT_INFLATER_SERVICE); 
            View music_popunwindwow = mLayoutInflater.inflate( 
                    R.layout.music_popwindow, null); 
            PopupWindow mPopupWindow = new PopupWindow(music_popunwindwow, LayoutParams.FILL_PARENT, 
                    LayoutParams.WRAP_CONTENT); 
             
            mPopupWindow.showAtLocation(findViewById(R.id.main), Gravity.RIGHT|Gravity.BOTTOM, 0, 0); 
        } 
    } 

需要强调的是这里PopupWindow必须有某个事件触发才会显示出来,不然总会抱错,不信大家可以试试!
随着这个问题的出现,就会同学问了,那么我想初始化让PopupWindow显示出来,那怎么办了,不去寄托于其他点击事件,在这里我用了定时器Time来实现这样的效果,当然这里就要用到Handler了下面是核心代码:

package com.android.tutor; 
import java.util.Timer; 
import java.util.TimerTask; 
import android.app.Activity; 
import android.content.Context; 
import android.os.Bundle; 
import android.os.Handler; 
import android.os.Message; 
import android.view.Gravity; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup.LayoutParams; 
import android.widget.PopupWindow; 
public class PopupWindowDemo extends Activity{ 
    private Handler mHandler = new Handler(){ 
         
        public void handleMessage(Message msg) { 
            switch (msg.what) { 
            case 1: 
                showPopupWindow(); 
                break; 
            } 
        }; 
    }; 
     
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
         
        //create the timer  
        Timer timer = new Timer(); 
        timer.schedule(new initPopupWindow(), 100); 
    } 
     
    private class initPopupWindow extends TimerTask{ 
        @Override 
        public void run() { 
             
            Message message = new Message(); 
            message.what = 1; 
            mHandler.sendMessage(message); 
             
        }        
    } 
     
     
    public void showPopupWindow() { 
        Context mContext = PopupWindowDemo.this; 
        LayoutInflater mLayoutInflater = (LayoutInflater) mContext 
                .getSystemService(LAYOUT_INFLATER_SERVICE); 
        View music_popunwindwow = mLayoutInflater.inflate( 
                R.layout.music_popwindow, null); 
        PopupWindow mPopupWindow = new PopupWindow(music_popunwindwow, 
                LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT); 
        mPopupWindow.showAtLocation(findViewById(R.id.main), Gravity.CENTER, 0, 0); 
    } 

package com.android.tutor; 
import java.util.Timer; 
import java.util.TimerTask; 
import android.app.Activity; 
import android.content.Context; 
import android.os.Bundle; 
import android.os.Handler; 
import android.os.Message; 
import android.view.Gravity; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup.LayoutParams; 
import android.widget.PopupWindow; 
public class PopupWindowDemo extends Activity{ 
    private Handler mHandler = new Handler(){ 
         
        public void handleMessage(Message msg) { 
            switch (msg.what) { 
            case 1: 
                showPopupWindow(); 
                break; 
            } 
        }; 
    }; 
     
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
         
        //create the timer  
        Timer timer = new Timer(); 
        timer.schedule(new initPopupWindow(), 100); 
    } 
     
    private class initPopupWindow extends TimerTask{ 
        @Override 
        public void run() { 
             
            Message message = new Message(); 
            message.what = 1; 
            mHandler.sendMessage(message); 
             
        }        
    } 
     
     
    public void showPopupWindow() { 
        Context mContext = PopupWindowDemo.this; 
        LayoutInflater mLayoutInflater = (LayoutInflater) mContext 
                .getSystemService(LAYOUT_INFLATER_SERVICE); 
        View music_popunwindwow = mLayoutInflater.inflate( 
                R.layout.music_popwindow, null); 
        PopupWindow mPopupWindow = new PopupWindow(music_popunwindwow, 
                LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT); 
        mPopupWindow.showAtLocation(findViewById(R.id.main), Gravity.CENTER, 0, 0); 
    } 

0_12735042167TMh.gif

下载 (10.5 KB)
2010-7-27 17:39


转自:http://blog.youkuaiyun.com/Android_Tutor/archive/2010/05/10/5576533.aspx

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值