一般项目中使用popupwindow如果只是当前页面特定的popup样式,我会直接写在当前的fragment或者activity中。如果有一个以上的地方需要展示同样的效果,我会对
改popupwindow进行一个封装,也就是自定义一个popupwindow,记录下项目中用到的一个支付popupwindow
这是效果图
popupwindow类
package com.daweiying.daweiying.weight.popupwindow; import android.app.ActionBar; import android.content.Context; import android.graphics.drawable.BitmapDrawable; import android.sax.RootElement; import android.util.Log; import android.view.Gravity; import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; import android.widget.ImageView; import android.widget.PopupWindow; import android.widget.RelativeLayout; import android.widget.TextView; import com.daweiying.daweiying.R; import com.daweiying.daweiying.base.Contant; /** * 支付popupwindow * Created by yujing on 2017/12/9 0009. */ public class BuyPopUpWindow extends PopupWindow implements View.OnClickListener{ private View mView; private ImageView iv_cancel; private TextView tv_price; private RelativeLayout rl_parent; private RelativeLayout rl_fjyf; private RelativeLayout rl_wallet; private RelativeLayout rl_wx; private ImageView iv_fjyf_check; private ImageView iv_wx_check; private ImageView iv_wallet_check; private TextView tv_pay; private OnBuySelectListener onBuySelectListener; public BuyPopUpWindow(Context context) { super(context); LayoutInflater mLayoutInflater = LayoutInflater.from(context); mView = (View)mLayoutInflater.inflate( R.layout.pop_buymethod, null);// 弹出窗口包含的视图 initView(mView); this.setContentView(mView); this.setAnimationStyle(R.style.dialogStyleAnimation); this.setWidth(ActionBar.LayoutParams.MATCH_PARENT); this.setHeight(ActionBar.LayoutParams.MATCH_PARENT); this.setFocusable(true); this.setTouchable(true); this.setOutsideTouchable(true); this.setBackgroundDrawable(new BitmapDrawable()); this.setTouchInterceptor(new View.OnTouchListener() { @Override public boolean onTouch(View arg0, MotionEvent arg1) { // TODO Auto-generated method stub return false; } }); } private void initView(View mView){ iv_cancel= (ImageView) mView.findViewById(R.id.iv_cancel); tv_price= (TextView) mView.findViewById(R.id.tv_price); rl_fjyf= (RelativeLayout) mView.findViewById(R.id.rl_fjyf); rl_wallet= (RelativeLayout) mView.findViewById(R.id.rl_wallet); rl_wx= (RelativeLayout) mView.findViewById(R.id.rl_wx); iv_fjyf_check= (ImageView) mView.findViewById(R.id.iv_fjyf_check); iv_wallet_check= (ImageView) mView.findViewById(R.id.iv_wallet_check); iv_wx_check= (ImageView) mView.findViewById(R.id.iv_wx_check); rl_parent= (RelativeLayout) mView.findViewById(R.id.rl_parent); tv_pay= (TextView) mView.findViewById(R.id.tv_pay); setListener(); } private void setListener(){ iv_cancel.setOnClickListener(this); rl_parent.setOnClickListener(this); rl_wallet.setOnClickListener(this); rl_wx.setOnClickListener(this); rl_fjyf.setOnClickListener(this); tv_pay.setOnClickListener(this); } /** * 设置价格 * @param price */ public void setPrice(String price){ tv_price.setText("¥"+price); } public void showPop(View view){ this.showAtLocation(view, Gravity.BOTTOM, 0, 0); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.rl_parent: case R.id.iv_cancel: BuyPopUpWindow.this.dismiss(); break; case R.id.rl_fjyf: onBuySelectListener.onFjyfClick(); iv_fjyf_check.setImageResource(R.mipmap.buy_select); iv_wx_check.setImageResource(R.mipmap.buy_normal); iv_wallet_check.setImageResource(R.mipmap.buy_normal); break; case R.id.rl_wx: onBuySelectListener.onWxClick(); iv_fjyf_check.setImageResource(R.mipmap.buy_normal); iv_wx_check.setImageResource(R.mipmap.buy_select); iv_wallet_check.setImageResource(R.mipmap.buy_normal); break; case R.id.rl_wallet: onBuySelectListener.onWalletClick(); iv_fjyf_check.setImageResource(R.mipmap.buy_normal); iv_wx_check.setImageResource(R.mipmap.buy_normal); iv_wallet_check.setImageResource(R.mipmap.buy_select); break; case R.id.tv_pay: onBuySelectListener.onPay(); break; } } public void setOnBUySelectListener(OnBuySelectListener onBUySelectListener){ this.onBuySelectListener=onBUySelectListener; } //回调接口 public interface OnBuySelectListener{ void onFjyfClick(); void onWxClick(); void onWalletClick(); void onPay(); } }
定义了一个buypopupwindow的文件,在这个类的构造函数中写一些初始化的东西,包括设置布局,获取空间,设置监听
定义了一个initView()方法来获取控件,方法结尾调用了自己定义的一个setlistener()方法来设置点击事件监听。
因为popupwindow是在activity中调用的,必然我们需要跟popupwindow中的控件进行交互,在其里面的控件点击时我们
需要做一些响应的处理。比如点击确定支付的时候我们需要在activity中去进行支付,
这里在最下面写了个回调的接口,里面定义自己想要关联的一些方法。并且在某个控件点击时执行相应的接口方法。
最后在activity中使用的话只要
buyPopUpWindow=new BuyPopUpWindow(this); buyPopUpWindow.setOnBUySelectListener(this);//设置回调监听
buypopupWindow.show(view);就可以了
布局文件
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:id="@+id/rl_parent" android:background="@color/tranlateblack_3" android:layout_height="match_parent"> <RelativeLayout android:background="@color/white" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true"> <ImageView android:id="@+id/iv_cancel" android:layout_width="50dp" android:layout_height="50dp" android:scaleType="centerInside" android:src="@mipmap/buy_cancel" /> <TextView android:layout_width="wrap_content" android:layout_height="50dp" android:gravity="center" android:text="确认付款" android:textSize="16sp" android:textColor="@color/black" android:layout_centerHorizontal="true" /> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="@color/gray_ee" android:layout_below="@+id/iv_cancel" /> <TextView android:id="@+id/tv_price" android:layout_width="wrap_content" android:layout_height="80dp" android:textSize="20sp" android:layout_centerHorizontal="true" android:text="¥198.98" android:gravity="center" android:textColor="@color/black" android:layout_below="@+id/iv_cancel" /> <View android:layout_width="match_parent" android:layout_height="1dp" android:layout_marginLeft="10dp" android:background="@color/gray_ee" android:layout_below="@+id/tv_price" /> <TextView android:layout_below="@+id/tv_price" android:id="@+id/tv_paymethod" android:layout_width="match_parent" android:layout_height="40dp" android:text="付款方式:" android:paddingLeft="10dp" android:gravity="center_vertical" android:textColor="@color/black" /> <RelativeLayout android:id="@+id/rl_fjyf" android:layout_below="@+id/tv_paymethod" android:layout_width="match_parent" android:layout_height="50dp"> <View android:layout_width="match_parent" android:layout_height="1dp" android:layout_marginLeft="10dp" android:background="@color/gray_ee" android:layout_alignParentTop="true" /> <TextView android:text="付甲一方" android:drawableLeft="@mipmap/buy_fjyf" android:layout_centerVertical="true" android:drawablePadding="10dp" android:paddingLeft="10dp" android:textColor="@color/black" android:gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <ImageView android:id="@+id/iv_fjyf_check" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:scaleType="centerInside" android:layout_marginRight="10dp" android:layout_centerVertical="true" android:src="@mipmap/buy_select" /> <View android:layout_width="match_parent" android:layout_height="1dp" android:layout_marginLeft="10dp" android:background="@color/gray_ee" android:layout_alignParentBottom="true" /> </RelativeLayout> <RelativeLayout android:id="@+id/rl_wallet" android:layout_below="@+id/rl_fjyf" android:layout_width="match_parent" android:layout_height="50dp"> <TextView android:text="我的钱包" android:drawableLeft="@mipmap/buy_wallet" android:layout_centerVertical="true" android:drawablePadding="10dp" android:paddingLeft="10dp" android:textColor="@color/black" android:gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <ImageView android:id="@+id/iv_wallet_check" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:scaleType="centerInside" android:layout_marginRight="10dp" android:layout_centerVertical="true" android:src="@mipmap/buy_normal" /> <View android:layout_width="match_parent" android:layout_height="1dp" android:layout_marginLeft="10dp" android:background="@color/gray_ee" android:layout_alignParentBottom="true" /> </RelativeLayout> <RelativeLayout android:id="@+id/rl_wx" android:layout_below="@+id/rl_wallet" android:layout_width="match_parent" android:layout_height="50dp"> <TextView android:text="微信支付" android:drawableLeft="@mipmap/buy_wx" android:layout_centerVertical="true" android:drawablePadding="10dp" android:paddingLeft="10dp" android:textColor="@color/black" android:gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <ImageView android:id="@+id/iv_wx_check" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:scaleType="centerInside" android:layout_marginRight="10dp" android:layout_centerVertical="true" android:src="@mipmap/buy_normal" /> <View android:layout_width="match_parent" android:layout_height="1dp" android:layout_marginLeft="10dp" android:background="@color/gray_ee" android:layout_alignParentBottom="true" /> </RelativeLayout> <TextView android:id="@+id/tv_pay" android:layout_below="@+id/rl_wx" android:layout_marginTop="20dp" android:layout_marginBottom="20dp" android:layout_width="match_parent" android:layout_height="50dp" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:textColor="@color/white" android:textSize="16sp" android:text="立即支付" android:gravity="center" android:background="@drawable/app_themecolor_bg" /> </RelativeLayout> </RelativeLayout>