android 弹出框菜单效果

java源代码


public class PopMenu {
	
	private  LinearLayout popmenu;
	private  PopupWindow mPopupwindow;
	
	private  OnClickListener ocl;
	public  boolean isShow = false;
	private View mAnchor;
	TextView sort_by_name;
	TextView sort_by_counter;
	TextView sort_by_date;
	Activity mActivity;
	static PopMenu  mpopMenu;
	
	private PopMenu(Activity activity, View anchor)
	{
		mAnchor = anchor;
		mActivity = activity;
		setupView();
	}
	private void setupView()
	{
		popmenu = (LinearLayout) mActivity.getLayoutInflater().inflate(R.layout.popmenu, null, false);
		sort_by_name = (TextView) popmenu.findViewById(R.id.sort_by_name);
		sort_by_counter= (TextView) popmenu.findViewById(R.id.sort_by_counter);
		sort_by_date = (TextView) popmenu.findViewById(R.id.sort_by_date);
		
		ocl = new OnClickListener()
		{
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				PopMenu.this.dimss();
				
				switch(v.getId())
				{
					default:
							break;
				}
			}
			
		};

		OnKeyListener okl =  new OnKeyListener()
		{

			@Override
			public boolean onKey(View arg0, int arg1, KeyEvent arg2) {
				// TODO Auto-generated method stub
				switch(arg1)
				{
				case KeyEvent.KEYCODE_BACK:
					dimss(); 
					break;
				default:
					break;
				}
				return false;
			}

		};

		sort_by_name.setFocusableInTouchMode(true);
		popmenu.setOnKeyListener(okl);
		sort_by_name.setOnKeyListener(okl);
		sort_by_counter.setOnKeyListener(okl);
		sort_by_date.setOnKeyListener(okl);
		sort_by_name.setOnClickListener(ocl);
		sort_by_counter.setOnClickListener(ocl);
		sort_by_date.setOnClickListener(ocl);

		mPopupwindow = new PopupWindow(popmenu, 320 ,402);
		mPopupwindow.setFocusable(true);
		mPopupwindow.setOutsideTouchable(true);
		mPopupwindow.getContentView().setOnTouchListener(new OnTouchListener()
		{

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
		//		mPopupwindow.setFocusable(false);  
				if(!isInView(v,event))
					dimss();
				return false;
			}
			// 判断是否触摸点位于弹出菜单内
			private boolean isInView(View v, MotionEvent event)
			{
				TextView sort = sort_by_name;
				Rect rect = new Rect();
				sort.getGlobalVisibleRect(rect);
				
				if(event.getRawX() > rect.left && event.getRawX() < rect.right
						&& (event.getRawY()-rect.top) > sort.getTop() && event.getRawY() < rect.bottom)
						return true;
				else
					return false;
			}
			
		});
		
	}
	public static PopMenu getPopMenu(Activity activity, View anchor)
	{
		if(mpopMenu == null)
		{	
			mpopMenu = new PopMenu(activity,anchor);
			return mpopMenu;
		}
		else
			return mpopMenu;
	}

	public  boolean dimss()
	{
		if(mPopupwindow.isShowing())
			mPopupwindow.dismiss();
		isShow = false;
		return false;
	}
	 public boolean show()
	{
		if(!mPopupwindow.isShowing())
		{		
			
			mPopupwindow.showAsDropDown(mAnchor);
			isShow = true;
			if(sort_by_name.isInTouchMode())
				sort_by_name.requestFocusFromTouch();
			else
				sort_by_name.requestFocus();
			
			//设置父窗口也可获得焦点	
			WindowManager wm =  (WindowManager) mActivity.getSystemService(Context.WINDOW_SERVICE);
			WindowManager.LayoutParams p = (WindowManager.LayoutParams)popmenu.getLayoutParams();
			p.flags |= WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
			wm.updateViewLayout(popmenu, p);
			
			return true;
		}	
		else
		{
			dimss();
		}
		return false;
	}
}


布局文件<pre name="code" class="html"><?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/popmenu"
    android:layout_width="320dip"
    android:layout_height="wrap_content"
    android:orientation="vertical" 
    >
    
    <TextView
        android:id="@+id/sort_by_name"
        android:layout_width="320dip"
        android:layout_height="100dp"
        android:layout_weight="1"
        android:gravity="center"
        android:text="test1"
        android:textSize="26dp" />
    

    <TextView
        android:id="@+id/sort_by_counter"
 		android:layout_width="320dip"
        android:layout_height="100dp"
        android:layout_weight="1"
        android:focusable="true"
        android:gravity="center"
        android:text="test2"
        android:textSize="26dp" />

    <TextView
        android:id="@+id/sort_by_date"
 		android:layout_width="320dip"
        android:layout_height="100dp"
        android:layout_weight="1"
        android:focusable="true"
        android:gravity="center"
        android:text="test3"
        android:textSize="26dp" />

</LinearLayout>


 

参与评论 您还未登录,请先 登录 后发表或查看评论

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

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:大白 设计师:CSDN官方博客 返回首页

打赏作者

csuxsh

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值