在我们的项目中,经常需要做不同的弹窗提示,当然 DIalog也可以自定义各种样式,但是我更倾向于popwindow;
我来保存一下我平时的用法:
封装一个class,在使用的时候调用,popindow中的点击事件通过 接口回调传给宿主;
下面是封装的简单的popwindow
public class Popwindow_Tip {
View view;
PopupWindow mPopupWindow;
private Activity activity;
//控件
TextView mTextview_cancle;
TextView mTextview_submit;
TextView mTextview_tip;
//对象
Interence_complteTask interence_complteTask;
String tip="";
public Popwindow_Tip(String tip,Activity activity,Interence_complteTask interence_complteTask) {
this.tip=tip;
this.activity = activity;
this.interence_complteTask=interence_complteTask;
}
public void showPopwindow(){
view= LayoutInflater.from(activity).inflate(R.layout.popwindow_tip,null,false);
mPopupWindow=new PopupWindow(view, (int) (Staticdata.ScreenWidth*0.8), ViewGroup.LayoutParams.WRAP_CONTENT);
mPopupWindow.setOutsideTouchable(true);// 触摸popupwindow外部,popupwindow消失
mPopupWindow.setFocusable(true);//设置焦点在window上,较为重要
mPopupWindow.setAnimationStyle(R.style.popissue_animation);//设置显示时候的动画
mPopupWindow.showAtLocation(activity.getWindow().getDecorView(), Gravity.CENTER, 0, 0);//设置显示的位置
Utils.setAlpha((float) 0.3,activity);
initview();
initlistenner();
}
private void initlistenner() {
mTextview_cancle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
interence_complteTask.onResult(false);
mPopupWindow.dismiss();
}
});
mTextview_submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
interence_complteTask.onResult(true);
mPopupWindow.dismiss();
}
});
mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
Utils.setAlpha((float) 1,activity);
}
});
}
private void initview() {
mTextview_cancle=view.findViewById(R.id.textview_cancle);
mTextview_submit=view.findViewById(R.id.textview_submit);
mTextview_tip=view.findViewById(R.id.textview_text);
mTextview_tip.setText(tip);
}
}
xml文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:background="@drawable/background_white_corners"
>
<TextView
android:id="@+id/textview_text"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:text=""
android:gravity="center"
android:textColor="@color/text_title2"
android:textSize="15dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
/>
<ImageView
android:id="@+id/image_q"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/gray_background"
android:layout_below="@id/textview_text"
android:layout_marginTop="20dp"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="@+id/image_q"
>
<TextView
android:id="@+id/textview_cancle"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:text="取消"
android:gravity="center"
/>
<ImageView
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="@color/gray_background"
/>
<TextView
android:id="@+id/textview_submit"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:text="确认"
android:gravity="center"
/>
</LinearLayout>
</RelativeLayout>
用到的接口:
public interface Interence_complteTask {
public void onResult(boolean result);
}
上面代码中调用的方法解释:
//设置背景遮罩颜色
public static void setAlpha(float bgAlpha, Activity activity) {
WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
lp.alpha = bgAlpha; //0.0-1.0
activity.getWindow().setAttributes(lp);
}