Android在初始化时弹出popwindow的方法

Android中在onCreate()时弹出popwindow,很多人都有过类似的需求吧,但是直接在onCreate()中调用popwindow的showAtLocation()方法是会报异常的,原因是此时activity还没有初始化完毕,网上也有一些解决方法,但一般都是通过延时一小段时间再弹出实现的,代码不够健壮。其实可以通过不断的检测当前activity的状态,一旦初始化完毕立即调用popwindow的showAtLocation()方法显示。详细代码如下:

  1. /************************************************************************ 
  2.  * 
  3.  * 该类用来在初始化的时候弹出一个popwindow,用到的而已文件如下,特别要注意的是其中的  
  4.  * android:minHeight="1dp" 和android:minWidth="1dp"属性。初始化时弹出popwindow的关键是 
  5.  * 要等待整个activity初始化完毕后再调用showAtLocation()方法,否则会出异常。 
  6.  * 实现方式就是不停的检测acitvity是否初始化完毕,一旦完毕就调用 
  7.  * showAtLocation() 方法显示popwindow。 
  8.  * 
  9.  * 以下为布局文件: 
  10.  *  <?xml version="1.0" encoding="utf-8"?> 
  11.  *  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  12.  *      android:id="@+id/main" 
  13.  *      android:layout_width="fill_parent" 
  14.  *      android:layout_height="fill_parent" 
  15.  *      android:minHeight="1dp" 
  16.  *      android:minWidth="1dp" 
  17.  *      android:orientation="vertical" > 
  18.  *  </LinearLayout> 
  19.  * 
  20.  * 作者:石兴 
  21.  * 完成时间:2012/3/10 
  22.  * 版本 :1.0 
  23.  * 
  24.  ************************************************************************/  
  25.   
  26. package cn.sd.sx.initpopw;  
  27.   
  28. import android.app.Activity;  
  29. import android.graphics.Color;  
  30. import android.os.Bundle;  
  31. import android.os.Handler;  
  32. import android.view.Gravity;  
  33. import android.view.View;  
  34. import android.view.ViewGroup.LayoutParams;  
  35. import android.widget.LinearLayout;  
  36. import android.widget.PopupWindow;  
  37. import android.widget.TextView;  
  38. import cn.sd.sx.popw.R;  
  39.   
  40. public class PopWActivity extends Activity {  
  41.       
  42.     private PopupWindow mPopupWindow;  
  43.     private Handler mHandler;  
  44.     // 检测时间间隔  
  45.     private int detchTime = 5;  
  46.       
  47.     @Override  
  48.     public void onCreate(Bundle savedInstanceState) {  
  49.         super.onCreate(savedInstanceState);  
  50.         setContentView(R.layout.main);  
  51.         mHandler = new Handler();  
  52.         // 显示popWindow  
  53.         showPopWindow();  
  54.     }  
  55.   
  56.     private void showPopWindow()  
  57.     {  
  58.         LinearLayout view = new LinearLayout(this);  
  59.         LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT,  
  60.                 LayoutParams.FILL_PARENT);  
  61.         TextView txt1 = new TextView(this);  
  62.         txt1.setLayoutParams(params);  
  63.         txt1.setGravity(Gravity.CENTER);  
  64.         txt1.setText("txt1");  
  65.         txt1.setBackgroundColor(Color.RED);  
  66.         view.addView(txt1);  
  67.         mPopupWindow = new PopupWindow(view, 300,300);  
  68.         mPopupWindow.setOutsideTouchable(true);  
  69.           
  70.         /*****************以下代码用来循环检测activity是否初始化完毕***************/  
  71.         Runnable showPopWindowRunnable = new Runnable() {  
  72.               
  73.             @Override  
  74.             public void run() {  
  75.                 // 得到activity中的根元素  
  76.                 View view = findViewById(R.id.main);  
  77.                 // 如何根元素的width和height大于0说明activity已经初始化完毕  
  78.                 if( view != null && view.getWidth() > 0 && view.getHeight() > 0) {  
  79.                     // 显示popwindow  
  80.                     mPopupWindow.showAtLocation(PopWActivity.this.findViewById(R.id.main),  
  81.                             Gravity.CENTER, 00);  
  82.                     // 停止检测  
  83.                     mHandler.removeCallbacks(this);  
  84.                 } else {  
  85.                     // 如果activity没有初始化完毕则等待5毫秒再次检测  
  86.                     mHandler.postDelayed(this, detchTime);  
  87.                 }  
  88.             }  
  89.         };  
  90.         // 开始检测  
  91.         mHandler.post(showPopWindowRunnable);  
  92.       /******************以上代码用来循环检测activity是否初始化完毕*************/  
  93.     }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,可以通过弹一个带有指示器的PopupWindow来实现。 在VerticalSeekBar类中添加以下代码: ```java private PopupWindow popupWindow; private TextView textView; public void showIndicator(int progress) { if (popupWindow == null) { textView = new TextView(getContext()); textView.setTextColor(Color.WHITE); textView.setTextSize(14); textView.setGravity(Gravity.CENTER); textView.setBackgroundResource(R.drawable.seekbar_indicator_bg); popupWindow = new PopupWindow(textView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); popupWindow.setOutsideTouchable(true); popupWindow.setFocusable(false); } int[] location = new int[2]; getLocationOnScreen(location); int thumbTop = (int) ((getHeight() - getThumb().getIntrinsicHeight()) * 0.5f); int thumbLeft = (int) ((getWidth() - getThumb().getIntrinsicWidth()) * 0.5f); int thumbWidth = getThumb().getIntrinsicWidth(); int thumbHeight = getThumb().getIntrinsicHeight(); int indicatorWidth = dp2px(getContext(), 40); int indicatorHeight = dp2px(getContext(), 20); int indicatorX = location[0] - indicatorWidth - thumbWidth; int indicatorY = location[1] + (getHeight() - indicatorHeight) * progress / getMax() - thumbTop; textView.setText(String.valueOf(progress)); popupWindow.showAtLocation(this, Gravity.NO_GRAVITY, indicatorX, indicatorY); } public void hideIndicator() { if (popupWindow != null) { popupWindow.dismiss(); } } private int dp2px(Context context, int dp) { float density = context.getResources().getDisplayMetrics().density; return (int) (dp * density + 0.5f); } ``` 在Activity中,可以这样使用: ```java VerticalSeekBar verticalSeekBar = findViewById(R.id.vertical_seekbar); verticalSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { verticalSeekBar.showIndicator(progress); } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onStopTrackingTouch(SeekBar seekBar) { verticalSeekBar.hideIndicator(); } }); ``` 在onProgressChanged()方法中弹PopupWindow,并在PopupWindow显示进度值。在onStopTrackingTouch()方法中隐藏PopupWindow。 这样就可以通过弹PopupWindow在滑块旁边显示进度值了。PopupWindow的位置使用了SeekBar的getLocationOnScreen()方法和一些计算。PopupWindow的大小为40dp * 20dp,背景使用了一个Drawable资源。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值