android之popupwindow

popwindow.xml

C-sharp代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.   xmlns:android="http://schemas.android.com/apk/res/android"  
  4.   android:orientation="vertical"  
  5.   android:layout_width="fill_parent"  
  6.   android:layout_height="fill_parent">  
  7. <TextView  
  8.     android:id="@+id/textview"  
  9.     android:layout_width="fill_parent"  
  10.     android:layout_height="wrap_content"  
  11.     android:text=""  
  12.     />  
  13. <Button  
  14.     android:id="@+id/test"  
  15.     android:layout_width="fill_parent"  
  16.     android:layout_height="wrap_content"  
  17.     android:text="测试"  
  18.     />  
  19. </LinearLayout>  

main.xml

Java代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@+id/main"  
  4.     android:orientation="vertical"  
  5.     android:layout_width="fill_parent"  
  6.     android:layout_height="fill_parent"  
  7.     >  
  8. <VideoView  
  9.     android:id="@+id/videoshow"  
  10.     android:layout_width="fill_parent"  
  11.     android:layout_height="fill_parent"  
  12.     />  
  13. </LinearLayout>  

Activity01.java

  1. package com.overflow.testvideo;  
  2.   
  3. import java.util.Timer;  
  4. import java.util.TimerTask;  
  5. import android.app.Activity;  
  6. import android.content.Context;  
  7. import android.net.Uri;  
  8. import android.os.Bundle;  
  9. import android.os.Handler;  
  10. import android.os.Message;  
  11. import android.view.Gravity;  
  12. import android.view.KeyEvent;  
  13. import android.view.LayoutInflater;  
  14. import android.view.MotionEvent;  
  15. import android.view.View;  
  16. import android.view.ViewGroup.LayoutParams;  
  17. import android.widget.Button;  
  18. import android.widget.MediaController;  
  19. import android.widget.PopupWindow;  
  20. import android.widget.TextView;  
  21. import android.widget.VideoView;  
  22. public class Activity01 extends Activity {  
  23.     VideoView vv = null;  
  24.     Button btn = null;  
  25.     TextView textview = null;  
  26.     /** Called when the activity is first created. */  
  27.     private Handler mHandler = new Handler(){  
  28.           
  29.         public void handleMessage(Message msg) {  
  30.             switch (msg.what) {  
  31.             case 1:  
  32.                 showPopupWindow();  
  33.                 break;  
  34.             }  
  35.         };  
  36.     };  
  37.     @Override  
  38.     public void onCreate(Bundle savedInstanceState) {  
  39.         super.onCreate(savedInstanceState);  
  40.           
  41.         setContentView(R.layout.main);  
  42.         Timer timer = new Timer();  
  43.         timer.schedule(new initPopupWindow(), 100);  
  44.         //textview = (TextView) music_popunwindwow.  
  45.         //btn = (Button) this.findViewById(R.id.test);  
  46.           
  47.         vv = (VideoView) this.findViewById(R.id.videoshow);  
  48.         vv.setVideoURI(Uri.parse("android.resource://com.overflow.testvideo/"+R.raw.underwater));    
  49.         vv.setMediaController(new MediaController(this));  
  50.         vv.start();  
  51.           
  52.         vv.setOnTouchListener(new VideoView.OnTouchListener() {  
  53.             @Override  
  54.             public boolean onTouch(View v, MotionEvent event) {  
  55.                 // TODO Auto-generated method stub  
  56.                 return true;  
  57.             }  
  58.               
  59.         });  
  60.           
  61.           
  62.           
  63.           
  64.     }  
  65.       
  66.     private class initPopupWindow extends TimerTask{  
  67.         @Override  
  68.         public void run() {  
  69.               
  70.             Message message = new Message();  
  71.             message.what = 1;  
  72.             mHandler.sendMessage(message);  
  73.               
  74.         }         
  75.     }  
  76.       
  77.       
  78.     public void showPopupWindow() {  
  79.         Context mContext = Activity01.this;  
  80.         LayoutInflater mLayoutInflater = (LayoutInflater) mContext  
  81.                 .getSystemService(LAYOUT_INFLATER_SERVICE);  
  82.         View music_popunwindwow = mLayoutInflater.inflate(  
  83.                 R.layout.popwindow, null);  
  84.         PopupWindow mPopupWindow = new PopupWindow(music_popunwindwow,  
  85.                 LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);  
  86.           
  87.         mPopupWindow.showAtLocation(findViewById(R.id.main), Gravity.CENTER, 0, 0);  
  88.           
  89.         btn = (Button) music_popunwindwow.findViewById(R.id.test);  
  90.         textview = (TextView) music_popunwindwow.findViewById(R.id.textview);  
  91.           
  92.         btn.setOnClickListener(new Button.OnClickListener() {  
  93.             @Override  
  94.             public void onClick(View v) {  
  95.                 // TODO Auto-generated method stub  
  96.                 textview.setText("测试成功");  
  97.             }  
  98.               
  99.         });  
  100.           
  101.           
  102.     }  
  103.   
  104.       
  105. }  

记得要dismiss(),否则返回会报错,难点在要触发popwindow上的事件,就必须是这个popwindow下来findviewbyid,否则就报错

本来这个东西的最初目的是写动态背景,但失败了,蛮多限制。贴上代码,以后可能需要。

还一些参考文章:http://www.cmd100.com/bbs/thread-111-1-1.html

http://mobile.51cto.com/android-254829.htm

posted on 2011-08-09 18:49  启程去旅行  阅读(382)  评论(0)    收藏  举报

导航