Android学习笔记(23) --- PopupWindow与手势GestureDetector的使用

一、最近遇到了一个问题,是关于Toast多次显示时会过慢的问题,系统自带的toast显示时间只有2秒或3秒两种,当然使用toast.cancel()的方法取消掉前面那个toast,但是如果多次点击显示toast,那么有一段时间是什么都没有显示的,最后只显示最后点击显示的toast。这远远达不到要求。在网上搜了下相关的解决方法:

1、使用反射机制,有这么一个例子:

Toast toast = Toast.makeText(this, "永不关闭的Toast", Toast.LENGTH_LONG);
		toast.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL, 0, 0);
		try {
			// 从Toast对象中获得mTN变量
			Field field = toast.getClass().getDeclaredField("mTN");
			field.setAccessible(true);
			Object obj = field.get(toast);
			// TN对象中获得了show方法
			Method method = obj.getClass().getDeclaredMethod("show", null);
			// TN对象中获得了show方法
			// Method method = obj.getClass().getDeclaredMethod("hide", null);

			// 调用show方法来显示Toast信息提示框
			method.invoke(obj, null);
		} catch (Exception e) {
		}
问题是上面的代码对android3.0以上的版本无效。无奈,现在想弄的是4.0。最后请教了一位朋友,给我指点了迷津,那就是使用PopupWindow来实现toast的效果。这样我想什么时候隐藏或者显示都行。


二、下面就是PopupWindow的使用,由于PopupWindow需要触发事件,所以我加上了手势触发,具体如下:

.java文件

package com.viewgroup;

import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.media.SoundPool;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

public class ViewgroupActivity extends Activity implements OnTouchListener,
		OnGestureListener, SoundPool.OnLoadCompleteListener {
	/** Called when the activity is first created. */
	private Button showtoast, closetoast;
	PopupWindow mPopupWindow = null;
	private GestureDetector mGestureDetector;
	int i = 0;
	TextView mtext;

	// private final Handler mHandler = new MyHandler();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		mGestureDetector = new GestureDetector(this);
		LinearLayout mlayout = (LinearLayout) findViewById(R.id.mainID);
		// 添加手势
		mlayout.setOnTouchListener(this);
		mlayout.setLongClickable(true);

		showtoast = (Button) findViewById(R.id.bt00);
		closetoast = (Button) findViewById(R.id.bt01);
		
		LayoutInflater mLayoutInflater = getLayoutInflater();
		View music_popunwindwow = mLayoutInflater.inflate(R.layout.text, null);
		mtext = (TextView) music_popunwindwow.findViewById(R.id.mtext);
		mtext.setText("toast->PopupWindow--->你好");
		mPopupWindow = new PopupWindow(music_popunwindwow,
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		showtoast.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

				mPopupWindow.showAtLocation(findViewById(R.id.mainID),
						Gravity.CENTER, 0, 0);
			}
		});
		closetoast.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				mPopupWindow.dismiss();
			}
		});
	}

	@Override
	public boolean onDown(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
		// TODO Auto-generated method stub
		//左右滑屏时说触发的事件
		if (i % 2 == 0) {
			i++;
			mPopupWindow.dismiss();
			mtext.setText("aa");
			mPopupWindow.showAtLocation(findViewById(R.id.mainID),
					Gravity.CENTER, 0, 0);
		} else {
			i++;
			mPopupWindow.dismiss();
			mtext.setText("bb");
			mPopupWindow.showAtLocation(findViewById(R.id.mainID),
					Gravity.CENTER, 0, 0);
		}

		return false;
	}

	@Override
	public void onLongPress(MotionEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
			float distanceY) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void onShowPress(MotionEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean onSingleTapUp(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		// TODO Auto-generated method stub
		return mGestureDetector.onTouchEvent(event);//这句话是关键,如果没有则手势会无效的
	}

	@Override
	public void onLoadComplete(SoundPool arg0, int arg1, int arg2) {
		// TODO Auto-generated method stub

	}
}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mainID"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ffffffff"
    android:orientation="vertical" >

    <Button
        android:id="@+id/bt00"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:text="show"/>

    <Button
        android:id="@+id/bt01"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="close" />
</LinearLayout>

text.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/mtext"
        android:text="hello"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30dp"
      />

</LinearLayout>

效果图就是滑屏一次显示文本,再滑屏一次则隐藏文本;或者通过按键控制它的显示或隐藏。

 

三、若想让popupwindow定时显示可以使用Timer,

menuText.setText(Constant_Src.menuName[currentPage]);
				mPopupWindow.showAtLocation(findViewById(R.id.mainID),
						Gravity.CENTER | Gravity.BOTTOM, 0, 0);
				mtimer.schedule(new TimerTask() {

					@Override
					public void run() {
						// TODO Auto-generated method stub
						mPopupWindow.dismiss();
						this.cancel();
					}
				}, 2000);//延时2秒开始执行

可参考博文:http://blog.csdn.net/moruna/article/details/7844426



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值