Android學習筆記(23) — PopupWindow與手勢GestureDetector的使用

來源:互聯網
上載者:User

一、最近遇到了一個問題,是關於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();@Overridepublic 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() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubmPopupWindow.showAtLocation(findViewById(R.id.mainID),Gravity.CENTER, 0, 0);}});closetoast.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubmPopupWindow.dismiss();}});}@Overridepublic boolean onDown(MotionEvent e) {// TODO Auto-generated method stubreturn false;}@Overridepublic 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;}@Overridepublic void onLongPress(MotionEvent e) {// TODO Auto-generated method stub}@Overridepublic boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,float distanceY) {// TODO Auto-generated method stubreturn false;}@Overridepublic void onShowPress(MotionEvent e) {// TODO Auto-generated method stub}@Overridepublic boolean onSingleTapUp(MotionEvent e) {// TODO Auto-generated method stubreturn false;}@Overridepublic boolean onTouch(View v, MotionEvent event) {// TODO Auto-generated method stubreturn mGestureDetector.onTouchEvent(event);//這句話是關鍵,如果沒有則手勢會無效的}@Overridepublic 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() {@Overridepublic void run() {// TODO Auto-generated method stubmPopupWindow.dismiss();this.cancel();}}, 2000);//延時2秒開始執行

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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.