<1>.能够点击popupwindow 内部空间,外部触摸消失,外部点击事件。

package myapplication.com.myapplication;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Display;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast; public class MainActivity extends AppCompatActivity implements PopupWindow.OnDismissListener { TextView textView_type;
int state = 0;
PopupWindow pop;
View layout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView_type = (TextView) findViewById(R.id.click);
popshop();
// popupwindows 外部的点击事件
TextView textView = (TextView) findViewById(R.id.text);
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "外部点击", Toast.LENGTH_SHORT).show();
}
}); } private void popshop() {
// textView_type= (TextView) view.findViewById(R.id.type_textview);
// mClassifyPopupWindow.showAtLocation(getActivity().
// findViewById(R.id.main), Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0); textView_type.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (state == 1 && pop.isShowing()) {
state = 0;
pop.dismiss();
} else {
/*** 弹出自定义的菜单 ***/
int height;
Display defaultDisplay = getWindowManager().getDefaultDisplay();
height = defaultDisplay.getHeight() / 10 * 7;
layout = View.inflate(MainActivity.this, R.layout.popwindow_layout, null);
//popweindows 里面可点击
TextView pop_textview1 = (TextView) layout.findViewById(R.id.pop_textview1);
pop_textview1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
pop.dismiss();
}
});
pop = new PopupWindow(layout, defaultDisplay.getWidth(),
height);
// ColorDrawable cd = new ColorDrawable(-0000);
// pop.setBackgroundDrawable(cd);
// pop.showAsDropDown(v); pop.update();
pop.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
pop.setTouchable(true); // 设置popupwindow可点击
pop.setOutsideTouchable(true); // 设置popupwindow外部可点击
// pop.setFocusable(true); // 获取焦点 这时外部不可点击触摸
/* 设置popupwindow的位置 */
pop.showAtLocation(layout,
(Gravity.BOTTOM - textView_type.getHeight())
| Gravity.LEFT, 0, 2 * textView_type.getHeight());
state = 1;
pop.setTouchInterceptor(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
pop.dismiss();
return true;
}
return false;
} }); }
}
}); } @Override
public void onDismiss() {
pop.dismiss(); } @Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub if (pop != null && pop.isShowing()) { pop.dismiss(); pop = null; } return super.onTouchEvent(event); } }

<2>.Mainacitivty.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="myapplication.com.myapplication.MainActivity"> <TextView
android:id="@+id/click"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:text="点击弹出PopWindows" /> <TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:text="pop弹出时仍能点击" />
</LinearLayout>

<3>. popwindow_layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffff5555" android:orientation="vertical">
<TextView
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="popwindows内部点击事件"
android:id="@+id/pop_textview1"
android:textSize="22dp"
android:gravity="center_horizontal"/>
<View
android:layout_height="2dp"
android:layout_marginTop="5dp"
android:layout_width="match_parent"
android:background="#000000" /> <myapplication.com.myapplication.XCFlowLayout
android:id="@+id/flowlayout"
android:layout_width="match_parent"
android:layout_height="match_parent" > </myapplication.com.myapplication.XCFlowLayout> </LinearLayout>

<4> 自定义菜单栏

pop的布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#6e6e6e"
android:orientation="vertical"> <TextView
android:id="@+id/textView1"
android:layout_width="140dp"
android:layout_height="40dp"
android:drawableLeft="@mipmap/yun"
android:gravity="center_horizontal|center_vertical"
android:text="菜单"
android:textColor="#000000"
android:textSize="17sp" /> <TextView
android:id="@+id/textView2"
android:layout_width="140dp"
android:layout_height="40dp"
android:drawableLeft="@mipmap/yun"
android:gravity="center_horizontal|center_vertical"
android:text="收藏"
android:textColor="#000000"
android:textSize="17sp" /> <TextView
android:id="@+id/textView3"
android:layout_width="140dp"
android:layout_height="40dp"
android:drawableLeft="@mipmap/yun"
android:gravity="center_horizontal|center_vertical"
android:text="爱好"
android:textColor="#000000"
android:textSize="17sp" />
</LinearLayout>

方法实现:

  /**
*
* @param view view点击事件,弹出popupwindow,view传入作为参数
*/
private void showPopupWindow_dingbu(View view) { /**
* 一个自定义的布局,作为显示的内容
*/ View contentView = LayoutInflater.from(MainActivity.this).inflate(R.layout.pop_layout, null); final PopupWindow popupWindow = new PopupWindow(contentView,
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, true);
WindowManager manager=(WindowManager) getSystemService(Context.WINDOW_SERVICE);
popupWindow.setTouchable(true);
popupWindow.setBackgroundDrawable(new BitmapDrawable());
popupWindow.setOutsideTouchable(true);
int xpos=manager.getDefaultDisplay().getWidth()/2-popupWindow.getWidth()/2; TextView textView3 = (TextView) contentView.findViewById(R.id.textView1);
/**
* 设置按钮的点击事件
*/
textView3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"ok",Toast.LENGTH_SHORT).show();
popupWindow.dismiss();
}
}); popupWindow.setTouchInterceptor(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return false;
// 这里如果返回true的话,touch事件将被拦截
// 拦截后 PopupWindow的onTouchEvent不被调用,这样点击外部区域无法dismiss
}
}); popupWindow.showAtLocation(view, Gravity.TOP|Gravity.RIGHT, 0, 140); //右下
popupWindow.showAsDropDown(view);
}

popupwindows的更多相关文章

  1. Android PopupWindows

    今天了解到PopupWindows这个布局,PopupWindow这个类用来实现一个弹出框,能够使用随意布局的View作为其内容,这个弹出框是悬浮在当前activity之上的. 以下是一个实例 xml ...

  2. PopupWindows 在2.3.3下报java.lang.NullPointerException

    03-05 01:20:56.040: E/AndroidRuntime(1396): java.lang.NullPointerException 03-05 01:20:56.040: E/And ...

  3. Anroid关于fragment控件设置长按事件无法弹出Popupwindows控件问题解决记录

    一.问题描述     记录一下最近在安卓的gragment控件中设置长按事件遇见的一个坑!!!     在正常的activity中整个活动中设置长按事件我通常实例化根部局,例如LinearLayout ...

  4. Android PopupWindows使用

    源码测试示例: package com.example.popupwindown; import android.os.Bundle; import android.app.Activity; imp ...

  5. 自定义PopupWindow

    PopupWindow,一个弹出窗口控件,可以用来显示任意View,而且会浮动在当前activity的顶部 自定义PopupWindow. 1.extends PopupWindow 2.构造方法中可 ...

  6. Dialog详解(包括进度条、PopupWindow、自定义view、自定义样式的对话框)

    Dialog详解(包括进度条.PopupWindow.自定义view.自定义样式的对话框)   Android中提供了多种对话框,在实际应用中我们可能会需要修改这些已有的对话框.本实例就是从实际出发, ...

  7. Android高手速成--第一部分 个性化控件(View)

    第一部分 个性化控件(View) 主要介绍那些不错个性化的View,包括ListView.ActionBar.Menu.ViewPager.Gallery.GridView.ImageView.Pro ...

  8. 据说年薪30万的Android程序员必须知道的帖子

    Android中国开发精英 目前包括: Android开源项目第一篇--个性化控件(View)篇       包括ListView.ActionBar.Menu.ViewPager.Gallery.G ...

  9. Android开源项目分类汇总

    目前包括: Android开源项目第一篇——个性化控件(View)篇   包括ListView.ActionBar.Menu.ViewPager.Gallery.GridView.ImageView. ...

随机推荐

  1. CorelDRAW购物节福利开奖 | “剁手节”前的攻略来了,请查收!

    距离“剁手节” 还有不到两周的时间 相信一些线上.线下.微商.电商...都拿出了看家本领进行市场混战 一场没有硝烟的战争开始啦!! …… 当然,我也不例外 你们谁都别拦着,我要打广告了 让你变成了不折 ...

  2. JSCH实现文件上传下载至sftp服务器

    文件服务器采用FreeSSHd,文件服务器配置就不细说了. 直接上代码,该代码可以直接使用. import com.jcraft.jsch.*; import java.io.InputStream; ...

  3. Linux的环境中如何生成srw-rw---- 的文件权限?

    文件属性 d 开头是: 目录文件. l 开头是: 符号链接(指向另一个文件,类似于瘟下的快捷方式). s 开头是: 套接字文件(sock). b 开头是: 块设备文件,二进制文件. c 开头是: 字符 ...

  4. Guava 是个风火轮之基础工具 (1)

    转自:http://www.jamespan.me/blog/2015/02/08/guava-basic-utilities-1/ Guava 是个风火轮之基础工具 (1) 前言 Guava 是 J ...

  5. 微信小程序:获取地理定位和显示相应的城市名称。

    最近在看微信小程序,遇到地理定位显示城市名称的问题.本文就是记录这一过程. 解决方案                                                          ...

  6. CF528D Fuzzy Search (生成函数+FFT)

    题目传送门 题目大意:给你两个只包含A,G,C,T的字符串$S$,$T$,$S$长$T$短,按照如下图方式匹配 解释不明白直接上图 能容错的距离不超过$K$,求能$T$被匹配上的次数 $S$串同一个位 ...

  7. Flask-SQLAlchemy中解决数据库连接1366报错

    报错信息:Warning: (1366, "Incorrect string value: '\\xD6\\xD0\\xB9\\xFA\\xB1\\xEA...' for column 'V ...

  8. Mysql错误:#1054 - Unknown column '字段名' in 'field list'

    # 1054 - Unknown column '字段名' in 'field list' 第一个就是你的表中没有这个字段 另一个就是你的这个字段前后可能有空格!!!,去掉空格即可!

  9. 基于ALSA的WAV播放和录音程序

    http://blog.csdn.net/azloong/article/details/6140824 这段时间在探索ALSA架构,从ALSA Core到ALSA Lib,再到Android Aud ...

  10. vue 如何动态切换组件,使用is进行切换

    日常项目中需要动态去切换组件进行页面展示. 例如:登陆用户是“管理员”或者“普通用户”,需要根据登陆的用户角色切换页面展示的内容.则需要使用 :is 属性进行绑定切换 <template> ...