实现Popuwindow

借鉴 http://www.eoeandroid.com/forum.php?mod=viewthread&tid=52125上提示的外部点击取消方法

代码如下:  实现点击Button出现popupwindow,再次点击取消popupwindow.点击popupwindow外部取消popupwindow,点击popupwindow中的item取消popupwindow

public class TestPopuwindowActivity extends Activity {
    /** Called when the activity is first created. */
	Button button;
	MyOnClick click ;
	PopupWindow popupWindow;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        button = (Button) findViewById(R.id.button);	
        button.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
					if(popupWindow!=null&&popupWindow.isShowing()) 
						popupWindow.dismiss();
					else 
						initPopWindow();  
			}
		});
    }
    
    /**
     * 点击popupwindow外部取消
     */
	@Override
	public boolean dispatchTouchEvent(MotionEvent ev) {
		if(popupWindow==null||!popupWindow.isShowing()) {
			return super.dispatchTouchEvent(ev);
		}
		boolean isOut = isOutOfBounds(ev);
		if(ev.getAction()==MotionEvent.ACTION_DOWN && isOut) {
			popupWindow.dismiss();
			return true;
		}
		return false;
	}

	/**
	 * 是否在popuwindow外部
	 * @param event 
	 * @return
	 */
	private boolean isOutOfBounds(MotionEvent event) {
		final int x=(int) event.getX();
        final int y=(int) event.getY();
        int slop = ViewConfiguration.get(TestPopuwindowActivity.this).getScaledWindowTouchSlop();
        View decorView = popupWindow.getContentView();
        return (x<-slop)||(y<-slop)
        ||(x>(decorView.getWidth()+slop))
        ||(y>(decorView.getHeight()+slop));
	}


	protected void initPopWindow() {
		LayoutInflater inflater = LayoutInflater.from(getApplicationContext());
		View view = inflater.inflate(R.layout.pup_item, null);
		popupWindow = new PopupWindow(findViewById(R.id.mainLayout), 230, 70);
		popupWindow.setContentView(view);
		ImageView img1 = (ImageView) view.findViewById(R.id.ImageView1);
		ImageView img2 = (ImageView) view.findViewById(R.id.imageView2);
		ImageView img3 = (ImageView) view.findViewById(R.id.imageView3);
		click = new MyOnClick();
		img1.setOnClickListener(click);
		img2.setOnClickListener(click);
		img3.setOnClickListener(click);
		popupWindow.setOutsideTouchable(true);
		popupWindow.setFocusable(false);
		popupWindow.showAsDropDown(button);
		
	}
	
	class MyOnClick implements View.OnClickListener {
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.ImageView1:
				Toast.makeText(getApplicationContext(), "1", 0).show();
				popupWindow.dismiss();
				break;
			case R.id.imageView2:
				Toast.makeText(getApplicationContext(), "2", 0).show();
				popupWindow.dismiss();
				break;
			case R.id.imageView3:
				Toast.makeText(getApplicationContext(), "3", 0).show();
				popupWindow.dismiss();
				break;
			}
		}
		
	}
}

实现效果图:实现Popuwindow_第1张图片

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" 
    android:id="@+id/mainLayout">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_weight="1" />
	<Button android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:id="@+id/button"
	    android:text="button"/>"
</LinearLayout>

pup_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#FFFFFF"
    >


    <ImageView
        android:id="@+id/ImageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />


    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />

    <ImageView
        android:id="@+id/imageView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />

</LinearLayout>

还差个按返回键的监听


@Override
	public void onBackPressed() {
		if(popupWindow==null||!popupWindow.isShowing()) {
			super.onBackPressed();
		}else {
			popupWindow.dismiss();
		}
	}


你可能感兴趣的:(android,layout,null,button,encoding)