自定义弹框一(PopupWindow实现) 响应两种点击事件
来源:互联网 发布:linux cp命令 编辑:程序博客网 时间:2024/05/17 04:00
自定义弹框通常可以使用 自定义的popupwindow,自定义的activity,自定义的dialog来实现 这里先用popupwindow来实现 并且可以在弹框上实现点击事件 或者在其所依附的activity上响应 其实都是一样的 但是如果点击弹框想在activity上处理事件 这种就比较简单了 避免了使用接口对接
第一种 在弹框上响应点击事件
先准备资源文件
alert_dialo.xml
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:orientation="vertical" > <LinearLayout android:id="@+id/pop_layout" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:orientation="vertical" android:layout_alignParentBottom="true" android:background="@drawable/btn_style_alert_dialog_background" > <Button android:id="@+id/btn_take_photo" android:layout_marginTop="20dip" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="拍照" android:background="@drawable/btn_style_alert_dialog_button" android:textStyle="bold" /> <Button android:id="@+id/btn_pick_photo" android:layout_marginTop="5dip" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="从相册选择" android:background="@drawable/btn_style_alert_dialog_button" android:textStyle="bold" /> <Button android:id="@+id/btn_cancel" android:layout_marginLeft="20dip" android:layout_marginRight="20dip" android:layout_marginTop="15dip" android:layout_marginBottom="15dip" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="取消" android:background="@drawable/btn_style_alert_dialog_cancel" android:textColor="#ffffff" android:textStyle="bold" /> </LinearLayout></RelativeLayout>
资源文件对应的类
package com.example.administrator.zidingyipupwindow;import android.app.Activity;import android.content.Context;import android.graphics.drawable.ColorDrawable;import android.view.LayoutInflater;import android.view.MotionEvent;import android.view.View;import android.view.View.OnClickListener;import android.view.View.OnTouchListener;import android.view.ViewGroup.LayoutParams;import android.widget.Button;import android.widget.PopupWindow;import android.widget.Toast;public class SelectPicPopupWindow extends PopupWindow { private Button btn_take_photo, btn_pick_photo, btn_cancel; private View mMenuView; public SelectPicPopupWindow(final Activity context, OnClickListener itemsOnClick) { super(context); LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); mMenuView = inflater.inflate(R.layout.alert_dialog, null); btn_take_photo = (Button) mMenuView.findViewById(R.id.btn_take_photo); btn_pick_photo = (Button) mMenuView.findViewById(R.id.btn_pick_photo); btn_cancel = (Button) mMenuView.findViewById(R.id.btn_cancel); //取消按钮 btn_cancel.setOnClickListener(new OnClickListener() { public void onClick(View v) { //销毁弹出框 dismiss(); } }); //设置按钮监听 btn_pick_photo.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Toast.makeText(context, "挑选照片", Toast.LENGTH_SHORT).show(); } }); btn_take_photo.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Toast.makeText(context, "拍照", Toast.LENGTH_SHORT).show(); } }); //设置SelectPicPopupWindow的View this.setContentView(mMenuView); //设置SelectPicPopupWindow弹出窗体的宽 this.setWidth(LayoutParams.FILL_PARENT); //设置SelectPicPopupWindow弹出窗体的高 this.setHeight(LayoutParams.WRAP_CONTENT); //设置SelectPicPopupWindow弹出窗体可点击 this.setFocusable(true); //设置SelectPicPopupWindow弹出窗体动画效果 this.setAnimationStyle(R.style.AnimBottom); //实例化一个ColorDrawable颜色为半透明 ColorDrawable dw = new ColorDrawable(0xb0000000); //设置SelectPicPopupWindow弹出窗体的背景 this.setBackgroundDrawable(dw); //mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框 mMenuView.setOnTouchListener(new OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { int height = mMenuView.findViewById(R.id.pop_layout).getTop(); int y=(int) event.getY(); if(event.getAction()==MotionEvent.ACTION_UP){ if(y<height){ dismiss(); } } return true; } }); }}
R.style.AnimBottom
<style name="AnimBottom" parent="android:Animation"> <item name="android:windowEnterAnimation">@anim/pop_enter_anim</item> <item name="android:windowExitAnimation">@anim/pop_exit_anim</item></style>@anim/pop_enter_anim<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"> <!--<translate--> <!--android:duration="450"--> <!--android:fromYDelta="100%p"--> <!--android:toYDelta="0" />--> <alpha android:duration="200" android:fromAlpha="0" android:toAlpha="1.0" /> // %p指相对于父容器 <translate android:fromYDelta="100%p" android:duration="300" /></set>@anim/pop_exit_anim这里面<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:duration="450" android:toYDelta="100%p"/> <!--<alpha--> <!--android:duration="200"--> <!--android:fromAlpha="1.0"--> <!--android:toAlpha="0.0" />--></set@anim/pop_exit_anim 和@anim/pop_exit_anim都是在新建的anim里下面是mainactivity里的资源文件activity_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="wrap_content" android:gravity="center_horizontal" android:id="@+id/main" android:orientation="vertical" > <TextView android:id="@+id/text" android:layout_width="50dip" android:layout_height="30dip" android:textSize="20sp" android:text="点我"/></LinearLayout>所对应的类 即是mainactivity里的东西package com.example.administrator.zidingyipupwindow;import android.app.Activity;import android.os.Bundle;import android.view.Gravity;import android.view.View;import android.view.View.OnClickListener;import android.widget.TextView;public class MainActivity extends Activity { //自定义的弹出框类 SelectPicPopupWindow menuWindow; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView tv = (TextView) this.findViewById(R.id.text); //把文字控件添加监听,点击弹出自定义窗口 tv.setOnClickListener(new OnClickListener() { public void onClick(View v) { //实例化SelectPicPopupWindow menuWindow = new SelectPicPopupWindow(MainActivity.this, itemsOnClick); //显示窗口 menuWindow.showAtLocation(MainActivity.this.findViewById(R.id.main), Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0); //设置layout在PopupWindow中显示的位置 } }); }}以上是点击事件在弹框里处理 既是相应的挑选 拍照吐司 里第二种第二种 方式 是点击事件在 activity中处理只需要在mainactivity中加上几句话 让其变为package com.example.administrator.zidingyipupwindow;import android.app.Activity;import android.os.Bundle;import android.view.Gravity;import android.view.View;import android.view.View.OnClickListener;import android.widget.TextView;public class MainActivity extends Activity { //自定义的弹出框类 SelectPicPopupWindow menuWindow; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView tv = (TextView) this.findViewById(R.id.text); //把文字控件添加监听,点击弹出自定义窗口 tv.setOnClickListener(new OnClickListener() { public void onClick(View v) { //实例化SelectPicPopupWindow menuWindow = new SelectPicPopupWindow(MainActivity.this, itemsOnClick); //显示窗口 menuWindow.showAtLocation(MainActivity.this.findViewById(R.id.main), Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0); //设置layout在PopupWindow中显示的位置 } }); } //为弹出窗口实现监听类 private OnClickListener itemsOnClick = new OnClickListener(){ public void onClick(View v) { menuWindow.dismiss(); switch (v.getId()) { case R.id.btn_take_photo:// Toast.makeText(MainActivity.this, "拍照---", Toast.LENGTH_SHORT).show(); break; case R.id.btn_pick_photo:// Toast.makeText(MainActivity.this, "嘻嘻---", Toast.LENGTH_SHORT).show(); break; default: break; } } };}并且 在SelectPicPopupWindow中 把设置按钮监听去掉 就OK了
1 0
- 自定义弹框一(PopupWindow实现) 响应两种点击事件
- PopupWindow点击事件响应备忘
- Android PopupWindow响应外部控件点击事件
- 点击popupwindow外部区域,点击事件的响应问题
- PopupWindow中使用GridView无法响应OnItemClickListener点击事件
- popupwindow放置listview,并且使listview可以响应点击事件
- popupwindow中的listview设置点击事件没响应
- popupwindow中使用ListView无法响应点击事件
- android popupwindow中使用ListView无法响应点击事件
- PopupWindow+ListView并实现点击事件
- 自定义ListView无法响应点击事件
- 自定义dialog响应按钮点击事件
- popupwindow中有editext,点击出来软键盘后,popupwindow没法响应键盘回退事件
- 自定义Cell上的点击事件两种方法
- RecyclerView的item点击事件(点击和长按)的两种实现
- PopupWindow点击监听事件
- Popupwindow属性点击事件
- Android响应按钮点击事件的4种方法实现
- mybatis快速入门及环境搭建
- excel,word,txt,img,pdf,转html
- 设置代理IP 设置完成后可发送抓取数据的请求
- Navicat11全系列激活(含注册机)
- kubernetes neutron网络架构方案实践
- 自定义弹框一(PopupWindow实现) 响应两种点击事件
- 二叉树的最大节点
- Android--百度地图报错:at com.baidu.location.LocationClient.onStart(Unknown Source)
- Android实现控件点击波纹扩散效果
- 关于Jquery WEUI Popup的笔记
- WARN com.mchange.v2.resourcepool.BasicResourcePool
- Laravel 创建可以传递参数 Console 服务
- mybatis配置文件常用语法mapper.xml
- 加速Android Studio编译