写下关于popwindow

本文介绍了一个Android应用中实现的视频播放功能及弹出窗口(popwindow)的代码实例。通过自定义弹出窗口并结合视频播放器使用,展示了如何响应触摸事件来显示和隐藏弹出窗口,并提供了必要的布局文件和Activity代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

popwindow.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"> <TextView android:id="@+id/textview" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="测试" /> <Button android:id="@+id/test" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="测试" /> </LinearLayout>

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="fill_parent" android:layout_height="fill_parent" > <VideoView android:id="@+id/videoshow" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </LinearLayout>

Activity01.java

package com.overflow.testvideo; import java.util.Timer; import java.util.TimerTask; import android.app.Activity; import android.content.Context; import android.net.Uri; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.Gravity; import android.view.KeyEvent; import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup.LayoutParams; import android.widget.Button; import android.widget.MediaController; import android.widget.PopupWindow; import android.widget.TextView; import android.widget.VideoView; public class Activity01 extends Activity { VideoView vv = null; Button btn = null; TextView textview = null; /** Called when the activity is first created. */ private Handler mHandler = new Handler(){ public void handleMessage(Message msg) { switch (msg.what) { case 1: showPopupWindow(); break; } }; }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Timer timer = new Timer(); timer.schedule(new initPopupWindow(), 100); //textview = (TextView) music_popunwindwow. //btn = (Button) this.findViewById(R.id.test); vv = (VideoView) this.findViewById(R.id.videoshow); vv.setVideoURI(Uri.parse("android.resource://com.overflow.testvideo/"+R.raw.underwater)); vv.setMediaController(new MediaController(this)); vv.start(); vv.setOnTouchListener(new VideoView.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { // TODO Auto-generated method stub return true; } }); } private class initPopupWindow extends TimerTask{ @Override public void run() { Message message = new Message(); message.what = 1; mHandler.sendMessage(message); } } public void showPopupWindow() { Context mContext = Activity01.this; LayoutInflater mLayoutInflater = (LayoutInflater) mContext .getSystemService(LAYOUT_INFLATER_SERVICE); View music_popunwindwow = mLayoutInflater.inflate( R.layout.popwindow, null); PopupWindow mPopupWindow = new PopupWindow(music_popunwindwow, LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT); mPopupWindow.showAtLocation(findViewById(R.id.main), Gravity.CENTER, 0, 0); btn = (Button) music_popunwindwow.findViewById(R.id.test); textview = (TextView) music_popunwindwow.findViewById(R.id.textview); btn.setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub textview.setText("测试成功"); } }); } }

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

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

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值