在最近做的项目中有碰到要写类似朋友圈的模块,因为要实现评论点赞功能,这里说下我是怎么实现评论功能的。
首先先放上效果图,,
这里贴上我的代码:
//给评论图标设置点击事件 mIv_header_discuss。setOnClickListener(新View.OnClickListener () { @Override 公共空间> 私人PopupWindow PopupWindow; 预展popupView=零; 私人EditText inputComment; 私人字符串nInputContentText; 私人TextView btn_submit; 私人RelativeLayout rl_input_container; 私人InputMethodManager mInputManager; @SuppressLint (“WrongConstant”) 私人空间showPopupcomment () { 如果(popupView==null) {//加载评论框的资源文件 popupView=LayoutInflater.from(上下文).inflate (R.layout.comment_popupwindow, null); } EditText inputComment=() popupView.findViewById (R.id.et_discuss); btn_submit=(按钮)popupView.findViewById (R.id.btn_confirm); rl_input_container=(使用)popupView.findViewById (R.id.rl_input_container);//利用计时器这个Api设置延迟显示软键盘,这里时间为200毫秒 计时器计时=new计时器(); 计时器。日程安排(新TimerTask () { 公共空间run () { mInputManager=(InputMethodManager) getActivity () .getSystemService (Context.INPUT_METHOD_SERVICE); mInputManager。showSoftInput (inputComment 0); } },200); 如果(popupWindow==null) { popupWindow=new popupWindow (popupView RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams。WRAP_CONTENT、假); }//popupWindow的常规设置,设置点击外部事件,背景色 popupWindow.setTouchable(真正的); popupWindow.setFocusable(真正的); popupWindow.setOutsideTouchable(真正的); popupWindow。setBackgroundDrawable(新ColorDrawable (0 x00000000)); popupWindow。setTouchInterceptor(新View.OnTouchListener () { @Override 公共布尔> popupWindow.setSoftInputMode (PopupWindow.INPUT_METHOD_NEEDED); popupWindow.setSoftInputMode (WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
这两句代码顺序不能变,然而这样写了之后还是实现不了,自己摸索了半天发现出现这样的问题与评论框的布局也有关系。
所以在这里贴上我的评论框布局
<强> R.layout.comment_popupwindow 强>
& lt; & # 63; xml version=" 1.0 " encoding=" utf - 8 " & # 63;比; & lt;使用 xmlns: android=" http://schemas.android.com/apk/res/android " android: id=癅 + id/rl_input_container” android: layout_width=" match_parent " android: layout_height=癿atch_parent”比; LinearLayout & lt; android: layout_width=" match_parent " android: layout_height=" 44 dp " android:背景=" @color/colorWhite” android: layout_alignParentBottom=" true " 面向android:="水平"比; EditText & lt; android: id=癅 + id/et_discuss” android: layout_width=" 0 dp” android: layout_weight=" 1 " android: layout_height=" 38 dp " android: textColorHint=" # a2a2a2” android: textSize=" 13 sp” android: layout_marginRight=" 7 dp " android: layout_marginLeft=" 15 dp " android: layout_marginBottom=" 6 dp " android: layout_marginTop=" 6 dp " android: ellipsize="结束" android:背景=" @drawable/round_edittext_input” android: layout_gravity=" center_vertical " android: paddingLeft=" @dimen/ten_padding” android: paddingRight=" @dimen/ten_padding” android:单行模式=" true "/比; & lt;按钮 android: id=癅 + id/btn_confirm” android:文本="发送” android:背景=" @drawable/btn_discuss_bg” android: textSize=" 16 sp " android: layout_gravity=" center_vertical " android: textColorHint=" # b7b7b7” android:输入textColor=" @color/colorWhite” android: layout_marginRight=" @dimen/ten_padding” android:重力="中心" android: layout_width=" 40 dp " android: layout_height=?8 dp/比; & lt;/LinearLayout> & lt;/RelativeLayout>
把评论框和发送按钮用LinearLayout包裹,然后在最外层用一个RelativeLayout包裹住,发现这样子评论框就会和软键盘一起弹出来了。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。