小茶楼装修效果图大全:写下关于popwindow

来源:百度文库 编辑:中财网 时间:2024/05/07 05:29:50

主要是代码,参考了http://blog.csdn.net/Android_Tutor/archive/2010/05/10/5576533.aspx

popwindow.xml

C-sharp代码

  1. "1.0" encoding="utf-8"?>   
  2.   xmlns:android="http://schemas.android.com/apk/res/android"  
  3.   android:orientation="vertical"  
  4.   android:layout_width="fill_parent"  
  5.   android:layout_height="fill_parent">   
  6.     android:id="@+id/textview"  
  7.     android:layout_width="fill_parent"  
  8.     android:layout_height="wrap_content"  
  9.     android:text="测试"  
  10.     />   

main.xml

Java代码

  1. "1.0" encoding="utf-8"?>   
  2. "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.     android:id="@+id/videoshow"  
  9.     android:layout_width="fill_parent"  
  10.     android:layout_height="fill_parent"  
  11.     />   
  12.   

Activity01.java

C-sharp代码

  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,否则就报错

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