Android自定义弹框Dialog和Popupwindow的封装使用

在项目开发中,经常会遇到各种通过弹框提示信息的需求,主要都是通过Dialog或者PopupWindow,由于在项目中可能需要多次用到,于是自己重写了2个类方便随时调用。

自定义Dialog类:

 

/**
 * author:xc
 * date: 2018/6/22
 * desc:自定义布局的Dialog
 */
public class CustomBaseDialog extends Dialog {
    /**
     * 布局文件
     */
    private View view;
    private int width;
    private int height;

    public CustomBaseDialog(Context context, int layoutResID, int width, int height) {
        super(context);
        LayoutInflater layoutInflater = LayoutInflater.from(context);
        view = layoutInflater.inflate(layoutResID, null);
        this.width = width;
        this.height = height;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(view, new ViewGroup.LayoutParams(width, height));
    }

    /**
     * @param viewRes  控件id
     * @param listener lister接口
     *                 控件点击事件
     */
    public void addViewOnclick(int viewRes, View.OnClickListener listener) {
        view.findViewById(viewRes).setOnClickListener(listener);
    }

    /**
     * @param viewRes 控件id
     * @param data    填充数据
     *                初始化数据
     */
    public void setTextViewData(int viewRes, String data) {
        TextView textView = (TextView) view.findViewById(viewRes);
        textView.setText(data);
    }
}

自定义PopupWindow类:

 

 

package com.satd.yshfq.widget;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.TextView;

import java.lang.reflect.Method;

/**
 * author:xc
 * date: 2018/6/22
 * desc:自定义布局的PopupWindow
 */
public class CustomBasePopupWindow extends PopupWindow {
    /**
     * 布局文件
     */
    private View view;

    /**
     * @param context     上下文
     * @param layoutResID 布局文件
     * @param width       窗口宽度
     * @param height      窗口高度
     */
    public CustomBasePopupWindow(Context context, int layoutResID, int width, int height) {
        view = LayoutInflater.from(context).inflate(layoutResID, null);
        setWidth(width);
        setHeight(height);
        setContentView(view);
        setFocusable(true);
        setBackgroundDrawable(new ColorDrawable(0x00000000));
    }

    /**
     * @param x 显示位置所在x坐标
     * @param y 显示位置所在y坐标
     */
    public void show(int x, int y) {
        showAtLocation(view, Gravity.NO_GRAVITY, x, y);
    }

    /**
     * 当点击外部不消失窗口,并且能相应外部控件的点击事件
     */
    public void setPopupWindowTouchModal(boolean touchModal) {
        Method method;
        try {
            method = PopupWindow.class.getDeclaredMethod("setTouchModal", boolean.class);
            method.setAccessible(true);
            method.invoke(this, touchModal);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * @param viewRes  控件id
     * @param listener lister接口
     *                 控件点击事件
     */
    public void addViewOnclick(int viewRes, View.OnClickListener listener) {
        view.findViewById(viewRes).setOnClickListener(listener);
    }

    /**
     * @param viewRes 控件id
     * @param data    填充数据
     *                初始化数据
     */
    public void setTextViewData(int viewRes, String data) {
        TextView textView = (TextView) view.findViewById(viewRes);
        textView.setText(data);
    }
}

 

 

 

 

 

 

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您介绍一下Android仿IOS自定义AlertDialog提示的实现方法。 首先,我们需要在Android项目中创建一个自定义布局文件,用于显示弹框的内容。可以使用LinearLayout或RelativeLayout等布局容器来组织弹框的内容,例如: ```xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/title" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="标题" android:textSize="18sp" /> <TextView android:id="@+id/message" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="内容" android:textSize="14sp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:gravity="center"> <Button android:id="@+id/confirm" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="确定" /> <Button android:id="@+id/cancel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="取消" /> </LinearLayout> </LinearLayout> ``` 接下来,我们需要创建一个自定义AlertDialog类,用于显示弹框和处理按钮点击事件。在这个类中,我们需要实现onCreateDialog方法来加载自定义布局文件,并设置弹框的标题、内容和按钮监听器等。例如: ```java public class IOSAlertDialog extends DialogFragment { private String title; private String message; private DialogInterface.OnClickListener confirmListener; private DialogInterface.OnClickListener cancelListener; public IOSAlertDialog(String title, String message, DialogInterface.OnClickListener confirmListener, DialogInterface.OnClickListener cancelListener) { this.title = title; this.message = message; this.confirmListener = confirmListener; this.cancelListener = cancelListener; } @Override public Dialog onCreateDialog(Bundle savedInstanceState) { LayoutInflater inflater = LayoutInflater.from(getActivity()); View view = inflater.inflate(R.layout.dialog_ios_alert, null); TextView titleView = view.findViewById(R.id.title); TextView messageView = view.findViewById(R.id.message); Button confirmButton = view.findViewById(R.id.confirm); Button cancelButton = view.findViewById(R.id.cancel); titleView.setText(title); messageView.setText(message); confirmButton.setOnClickListener(confirmListener); cancelButton.setOnClickListener(cancelListener); AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setView(view); return builder.create(); } } ``` 最后,在我们的Activity中,我们可以通过创建一个实例对象,并调用show方法来显示弹框。例如: ```java IOSAlertDialog dialog = new IOSAlertDialog( "提示", "确定要删除吗?", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // 确定按钮点击事件 } }, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // 取消按钮点击事件 } }); dialog.show(getSupportFragmentManager(), "IOSAlertDialog"); ``` 这样,我们就可以实现一个Android仿IOS自定义AlertDialog提示了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值