tv3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 一个自定义的布局,作为显示的内容 View contentView = LayoutInflater.from(context).inflate( R.layout.f2_popupwindow, null); // 设置按钮的点击事件 Button button = (Button) contentView.findViewById(R.id.button1); final PopupWindow popupWindow = new PopupWindow(contentView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { nationslist.remove(position); notifyDataSetChanged(); Toast.makeText(context, "已取消这条新闻", Toast.LENGTH_SHORT).show(); popupWindow.dismiss(); } }); popupWindow.setTouchable(true); popupWindow.setTouchInterceptor(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { Log.i("mengdd", "onTouch : "); return false; // 这里如果返回true的话,touch事件将被拦截 // 拦截后 PopupWindow的onTouchEvent不被调用,这样点击外部区域无法dismiss } }); // 设置好参数之后再show popupWindow.showAsDropDown(v); } });
<Button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="10dp" android:text="不感兴趣" android:textSize="20sp" />