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>
参与评论
您还未登录,请先
登录
后发表或查看评论
06-07
339

“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交