Android高手进阶教程(十)之----Android PopupWindow的使用

01.package com.android.tutor;  
02.import android.app.Activity;  
03.import android.content.Context;  
04.import android.os.Bundle;  
05.import android.view.Gravity;  
06.import android.view.LayoutInflater;  
07.import android.view.View;  
08.import android.view.View.OnClickListener;  
09.import android.view.ViewGroup.LayoutParams;  
10.import android.widget.Button;  
11.import android.widget.PopupWindow;  
12.public class PopupWindowDemo extends Activity  implements OnClickListener{  
13.    private Button btn;  
14.      
15.    public void onCreate(Bundle savedInstanceState) {  
16.        super.onCreate(savedInstanceState);  
17.        setContentView(R.layout.main);  
18.          
19.        btn = (Button)findViewById(R.id.btn);  
20.        btn.setOnClickListener(this);  
21.    }  
22.    @Override 
23.    public void onClick(View v) {  
24.        Context mContext = PopupWindowDemo.this;  
25.        if (v.getId() == R.id.btn) {  
26.            LayoutInflater mLayoutInflater = (LayoutInflater) mContext  
27.                    .getSystemService(LAYOUT_INFLATER_SERVICE);  
28.            View music_popunwindwow = mLayoutInflater.inflate(  
29.                    R.layout.music_popwindow, null);  
30.            PopupWindow mPopupWindow = new PopupWindow(music_popunwindwow, LayoutParams.FILL_PARENT,  
31.                    LayoutParams.WRAP_CONTENT);  
32.              
33.            mPopupWindow.showAtLocation(findViewById(R.id.main), Gravity.RIGHT|Gravity.BOTTOM, 0, 0);  
34.        }  
35.    }  
36.} 

需要强调的是这里PopupWindow必须有某个事件触发才会显示出来,不然总会抱错,不信大家可以试试!

随着这个问题的出现,就会同学问了,那么我想初始化让PopupWindow显示出来,那怎么办了,不去寄托于其他点击事件,

在这里我用了定时器Timer来实现这样的效果,当然这里就要用到Handler了


01.package com.android.tutor;  
02.import java.util.Timer;  
03.import java.util.TimerTask;  
04.import android.app.Activity;  
05.import android.content.Context;  
06.import android.os.Bundle;  
07.import android.os.Handler;  
08.import android.os.Message;  
09.import android.view.Gravity;  
10.import android.view.LayoutInflater;  
11.import android.view.View;  
12.import android.view.ViewGroup.LayoutParams;  
13.import android.widget.PopupWindow;  
14.public class PopupWindowDemo extends Activity{  
15.    private Handler mHandler = new Handler(){  
16.          
17.        public void handleMessage(Message msg) {  
18.            switch (msg.what) {  
19.            case 1:  
20.                showPopupWindow();  
21.                break;  
22.            }  
23.        };  
24.    };  
25.      
26.    public void onCreate(Bundle savedInstanceState) {  
27.        super.onCreate(savedInstanceState);  
28.        setContentView(R.layout.main);  
29.          
30.        //create the timer   
31.        Timer timer = new Timer();  
32.        timer.schedule(new initPopupWindow(), 100);  
33.    }  
34.      
35.    private class initPopupWindow extends TimerTask{  
36.        @Override 
37.        public void run() {  
38.              
39.            Message message = new Message();  
40.            message.what = 1;  
41.            mHandler.sendMessage(message);  
42.              
43.        }         
44.    }  
45.      
46.      
47.    public void showPopupWindow() {  
48.        Context mContext = PopupWindowDemo.this;  
49.        LayoutInflater mLayoutInflater = (LayoutInflater) mContext  
50.                .getSystemService(LAYOUT_INFLATER_SERVICE);  
51.        View music_popunwindwow = mLayoutInflater.inflate(  
52.                R.layout.music_popwindow, null);  
53.        PopupWindow mPopupWindow = new PopupWindow(music_popunwindwow,  
54.                LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);  
55.        mPopupWindow.showAtLocation(findViewById(R.id.main), Gravity.CENTER, 0, 0);  
56.    }  
57.}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    >
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Welcome to Mr Wei's Blog!!"
    />
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show PopupWindow"
/>
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hide PopupWindow"
/>
</LinearLayout>


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/Android_Tutor/archive/2010/05/10/5576533.aspx

你可能感兴趣的:(xml,android,.net,Blog,OS)