popupwindows
<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的更多相关文章
- Android PopupWindows
今天了解到PopupWindows这个布局,PopupWindow这个类用来实现一个弹出框,能够使用随意布局的View作为其内容,这个弹出框是悬浮在当前activity之上的. 以下是一个实例 xml ...
- 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 ...
- Anroid关于fragment控件设置长按事件无法弹出Popupwindows控件问题解决记录
一.问题描述 记录一下最近在安卓的gragment控件中设置长按事件遇见的一个坑!!! 在正常的activity中整个活动中设置长按事件我通常实例化根部局,例如LinearLayout ...
- Android PopupWindows使用
源码测试示例: package com.example.popupwindown; import android.os.Bundle; import android.app.Activity; imp ...
- 自定义PopupWindow
PopupWindow,一个弹出窗口控件,可以用来显示任意View,而且会浮动在当前activity的顶部 自定义PopupWindow. 1.extends PopupWindow 2.构造方法中可 ...
- Dialog详解(包括进度条、PopupWindow、自定义view、自定义样式的对话框)
Dialog详解(包括进度条.PopupWindow.自定义view.自定义样式的对话框) Android中提供了多种对话框,在实际应用中我们可能会需要修改这些已有的对话框.本实例就是从实际出发, ...
- Android高手速成--第一部分 个性化控件(View)
第一部分 个性化控件(View) 主要介绍那些不错个性化的View,包括ListView.ActionBar.Menu.ViewPager.Gallery.GridView.ImageView.Pro ...
- 据说年薪30万的Android程序员必须知道的帖子
Android中国开发精英 目前包括: Android开源项目第一篇--个性化控件(View)篇 包括ListView.ActionBar.Menu.ViewPager.Gallery.G ...
- Android开源项目分类汇总
目前包括: Android开源项目第一篇——个性化控件(View)篇 包括ListView.ActionBar.Menu.ViewPager.Gallery.GridView.ImageView. ...
随机推荐
- Python框架、库和软件资源大全(整理篇)
有少量修改,请访问原始链接.PythonWIn的exe安装包;http://www.lfd.uci.edu/~gohlke/pythonlibs/ 原文链接:codecloud.net/python- ...
- jquery的语法
$(this).hide() - 隐藏当前元素 $("p").hide() - 隐藏所有段落 $(".test").hide() - 隐藏所有 class=&q ...
- vc++如何创建程序-函数的重载
重载构成的条件:函数的参数类型,参数个数不同,才能构成函数的重载 函数重载分为两种情况: 1 .(1)void output(); (2)int output(); 2 .(1)void output ...
- 初级模拟电路:1-2 PN结与二极管
回到目录 1. 掺杂半导体 上面我们分析了本征半导体的导电情况,但由于本征半导体的导电能力很低,没什么太大用处.所以,一般我们会对本征半导体材料进行掺杂,即使只添加了千分之一的杂质,也足以改变半导 ...
- css超出不换行可滑动
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name ...
- MYSQL存储过程初步认知
存储过程(Stored Procedure): 一组可编程的函数,是为了完成特定功能的SQL语句集,经编译创建并保存在数据库中,用户可通过指定存储过程的名字并给定参数(需要时)来调用执行. 优点:将重 ...
- mobile touch 备用
var _scrollIndex=1; function scrollPage(){ var _contentEle = $('.view-container'),_viewEle = _conten ...
- 可序列化serializable的作用是什么
什么情况下需要序列化:a)当你想把的内存中的对象写入到硬盘的时候:b)当你想用套接字在网络上传送对象的时候: 为什么要序列化: 为了将对象可以以流的方式传输到其他位置,就必须要将该对象定义为可序列化的 ...
- JavaScript替换字符串中最后一个字符
1.问题背景 在一个输入框中,限制字符串长度为12位.利用键盘输入一个数字,会将字符串中最后一位替换,比方:111111111111.再输入一个3,会显示111111111113 2.详细实现 < ...
- 升级后开机就提示“android.process.acore”停止执行 --分析 解决方式
OTA升级的,升级引发的全部问题都是能够解释的,有的能解决,有的不能解决. 一个项目报了这个问题. 升级后开机就提示"android.process.acore"停止执行 抓取 a ...