安卓开发:模仿微信,QQ评论输入框,使用PopupWindow完美实现(新版)

首先说一下实现的效果,点击外部消失,有个缺点,键盘弹出时,点击back不会消失,第二次点击back或者点外部才可以消失;
我的直播项目里用的,有“弹幕”按钮(已隐藏),上效果
这里写图片描述
布局左边是有弹幕按钮的,隐藏了,有需要打开就可以了
上布局代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/popup_comment_parent"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:alpha="0.9"
    android:background="@color/white">
    <!-- 这是标记是否是弹幕 -->
    <CheckBox
        android:id="@+id/popup_comment_bull_cb"
        android:layout_width="24dp"
        android:layout_height="24dp"
        android:layout_centerVertical="true"
        android:layout_marginLeft="6dp"
        android:layout_marginRight="6dp"
        android:background="@drawable/tm_selector_bull"
        android:button="@null"
        android:checked="false"
        android:visibility="gone" />

    <View
        android:layout_width="1px"
        android:layout_height="34dp"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@+id/popup_comment_bull_cb"
        android:background="@color/tm_titlebar_div_line"
        android:visibility="gone" />

    <EditText
        android:id="@+id/popup_comment_edt"
        android:layout_width="match_parent"
        android:layout_height="44dp"
        android:layout_marginLeft="4dp"
        android:layout_marginRight="4dp"
        android:layout_toLeftOf="@+id/popup_comment_send_tv"
        android:layout_toRightOf="@+id/popup_comment_bull_cb"
        android:background="@null"
        android:gravity="center_vertical"
        android:hint="说点什么吧"
        android:imeOptions="actionNone"
        android:maxLength="30"
        android:paddingLeft="6dp"
        android:paddingRight="6dp"
        android:singleLine="true"
        android:textColor="@color/tm_black_tv"
        android:textColorHint="@color/tm_blue_dark"
        android:textCursorDrawable="@drawable/text_cursor_cyan"
        android:textSize="15dp" />

    <View
        android:layout_width="1px"
        android:layout_height="34dp"
        android:layout_centerVertical="true"
        android:layout_toLeftOf="@+id/popup_comment_send_tv"
        android:background="@color/tm_titlebar_div_line" />

    <TextView
        android:id="@+id/popup_comment_send_tv"
        android:layout_width="60dp"
        android:layout_height="44dp"
        android:layout_alignParentRight="true"
        android:gravity="center"
        android:text="发送"
        android:textColor="@color/tm_blue"
        android:textSize="16dp" />

</RelativeLayout>

重写控件继承自PopupWindow

class CommentPopupWindow extends PopupWindow

写一个回调来处理点击发送时的操作

public interface LiveCommentSendClick {
    //第二个参数标记是不是弹幕,第三个是评论内容
    void onSendClick(View view, boolean isBull, String comment);
}

声明控件

    //点击“发送”回调
    private LiveCommentSendClick sendClick;

    private View contentView;
    private CheckBox popupCommentBullCb;
    private EditText popupCommentEdt;
    private TextView popupCommentSendTv;

构造方法

    //构造方法
    public CommentPopupWindow(FragmentActivity context, LiveCommentSendClick sendClick) {
        super(context);
        this.context = context;
        this.sendClick = sendClick;
        foundPopup();
    }

    private void foundPopup() {
        contentView = View.inflate(context, R.layout.popup_comment_input, null);

        popupCommentParent = (RelativeLayout) contentView.findViewById(R.id.popup_comment_parent);
        popupCommentBullCb = (CheckBox) contentView.findViewById(R.id.popup_comment_bull_cb);
        popupCommentEdt = (EditText) contentView.findViewById(R.id.popup_comment_edt);
        popupCommentSendTv = (TextView) contentView.findViewById(R.id.popup_comment_send_tv);

        setContentView(contentView);
        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        //设置弹出动画
        setAnimationStyle(R.style.popWindow_animation_in2out);
        //使其聚集 ,要想监听菜单里控件的事件就必须要调用此方法
        setFocusable(true);
        //设置允许在外点击消失
        setOutsideTouchable(true);
        //这个是为了点击“Back”也能使其消失,不会影响背景
        setBackgroundDrawable(new BitmapDrawable());
        //显示在键盘上方
        setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

        popupCommentSendTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String result = popupCommentEdt.getText().toString().trim();
                if (result.length() <= 0) {
                    Toast.makeText(context, "还没有填写任何内容哦!", Toast.LENGTH_SHORT).show();
                } else {
                    //第二个参数标记是否是弹幕
                    sendClick.onSendClick(v, popupCommentBullCb.isChecked(), result);
                    dismiss();
                }
            }
        });

        setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                AppUtils.hideSoftInput(context, popupCommentEdt.getWindowToken());
            }
        });
    }

开放出一个方法供外部调用

    public void showReveal() {
        if (contentView == null) {
            Toast.makeText(context, "创建失败", Toast.LENGTH_SHORT).show();
        } else {
            //延时显示软键盘
            new Timer().schedule(new TimerTask() {
                @Override
                public void run() {
                    AppUtils.showKeyboard(popupCommentEdt);
                }
            }, 200);
            //显示并设置位置
            showAtLocation(contentView, Gravity.BOTTOM, 0, 0);
        }
    }

源码附上
http://download.csdn.net/download/ximen_qing/9955633

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值