popupwindow弹出的editTextView

PopupWindow与EditText交互实践
本文详细探讨了如何在Android应用中使用PopupWindow显示一个包含EditText的弹窗,并阐述了在弹窗中进行输入和交互的实现过程,包括焦点管理、软键盘处理等关键点。

public void getCommentView(final Activity context, View view, final CommentResult commentResult) {
        liveCommentResult = commentResult;
        if (commentView == null) {
            commentView = context.getLayoutInflater().inflate(R.layout.comment_question, null);
        }
        if (commentPopup == null) {
            // 创建一个PopuWidow对象
            commentPopup = new PopupWindow(commentView, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        }
        // 设置动画 commentPopup.setAnimationStyle(R.style.popWindow_animation_in2out);
        // 使其聚集 ,要想监听菜单里控件的事件就必须要调用此方法
        commentPopup.setFocusable(true);
        // 设置允许在外点击消失
        commentPopup.setOutsideTouchable(true);
        // 设置背景,这个是为了点击返回Back”也能使其消失,并且并不会影响你的背景
        commentPopup.setBackgroundDrawable(new BitmapDrawable());
        //必须加这两行,不然不会显示在键盘上方
        commentPopup.setSoftInputMode(PopupWindow.INPUT_METHOD_NEEDED);
        commentPopup.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        // PopupWindow的显示及位置设置
        commentPopup.showAtLocation(view, Gravity.BOTTOM, 0, 0);
        comment_text = (EditText) commentView.findViewById(R.id.comment_text);
        TextView send_comment = (TextView) commentView.findViewById(R.id.send_comment);

        comment_text.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() > 0) {
                    send_comment.setSelected(true);
                    send_comment.setEnabled(true);
                } else {
                    send_comment.setSelected(false);
                    send_comment.setEnabled(false);
                }
            }
        });
//这是布局中发送按钮的监听
        send_comment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String comment = comment_text.getText().toString();
                if (!TextUtils.isEmpty(comment)) {
                    if (TextUtils.isEmpty(comment.trim())) {
                        send_comment.setEnabled(false);
                    } else {
                        send_comment.setEnabled(true);
                        //把数据传出去
                        liveCommentResult.onResult(true, comment);
                        //关闭popup
                        commentPopup.dismiss();
                    }
                    Toast.makeText(getContext(), getResources().getString(R.string.comment_is_none), Toast.LENGTH_SHORT).show();
                }
            }
        });
        //设置popup关闭时要做的操作
        commentPopup.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                hideKeyboard(comment_text);
                comment_text.setText("");
            }
        });
        //显示软键盘
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                //此方法就不提供了,网上一大推
                showSoftKeyboard();
            }
        }, 200);
        //显示键盘
    }

    /**
     * 发送评论回调
     */
    public interface CommentResult {
        void onResult(boolean confirmed, String comment);
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值