需求实现:点击用户评论列表项,弹出输入法并进行编辑回复功能

来源:互联网 发布:唯美现代诗 知乎 编辑:程序博客网 时间:2024/06/10 18:06



工作日志记录:在项目开发的过程中,碰到一个需求就是在你点击用户回复列表的某一项的时候需要实现弹出输入法并输入评论然后发送评论内容的功能,现在将实现过程记录下来。


实现效果图:

上面左边是评论列表图,右边是输入法弹出的图。


实现思路:由于左图中是没有看见文本编辑框和发送按钮的,所以我需要将这连个控件放在底部并以不可见的状态存在于底部,当点击条目是,将这两个控件的状态置为可见,获取焦点并弹出输入法。


实现代码如下:


  //监听软键盘是否显示或隐藏,用于判断什么时候隐藏这个edittext和发送按钮        ll_inputmethod.getViewTreeObserver().addOnGlobalLayoutListener(                new ViewTreeObserver.OnGlobalLayoutListener() {                    @Override                    public void onGlobalLayout() {                        Rect r = new Rect();                        ll_inputmethod.getWindowVisibleDisplayFrame(r);                        int screenHeight = ll_inputmethod.getRootView()                                .getHeight();                        int heightDifference = screenHeight - (r.bottom);//                        Toast.makeText(activity, "heightDiff"+heightDifference, Toast.LENGTH_SHORT).show();                        if (heightDifference > 200) {                            //软键盘显示                            ll_inputmethod.setVisibility(View.VISIBLE);                        } else {                            //软键盘隐藏                            ll_inputmethod.setVisibility(View.GONE);                        }                        edit_comment.setText("");//初始化操作                    }                });

 /**     * EditText获取焦点并显示软键盘     */    public  void showSoftInputFromWindow(Activity activity, EditText editText,boolean hideOrNot) {        editText.setFocusable(true);        editText.setFocusableInTouchMode(true);        editText.requestFocus();//        activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);        //打开软键盘        InputMethodManager imm = (InputMethodManager)activity.getSystemService(Context.INPUT_METHOD_SERVICE);        if(hideOrNot){            imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);        }else{            imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);        }    }

发送按钮的点击事件:

case R.id.tv_send:    String comment=edit_comment.getText().toString().trim();    if(!TextUtils.isEmpty(comment)) {        if(commentId!=-1) {           //评论他人的评论        }else{          //评论该文章        }        showSoftInputFromWindow(activity,edit_comment,true);        ToastUtil.show(activity,"评论发布成功");    }else{        ToastUtil.show(activity,"评论不能为空");    }break;


评论列表的点击事件:

nelv_comment_detail为评论列表控件listview

nelv_comment_detail.setOnItemClickListener(new AdapterView.OnItemClickListener() {    @Override    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {  //显示输入法键盘,和edittext以及发送按钮 showSoftInputFromWindow(activity,edit_comment,false);        commentId=mAdapter.getCommentList().get(position).getCommentId();    }});edittext和发送按钮的布局文件(部分):<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"android:layout_height="match_parent"><listview.../>


<LinearLayout    android:layout_width="match_parent"    android:layout_height="30dp"    android:orientation="horizontal"    android:layout_alignParentBottom="true"    android:visibility="gone"    android:id="@+id/ll_inputmethod"    android:layout_marginBottom="@dimen/dimen_dp_5"    android:background="@color/white"    >    <EditText        android:layout_width="0dp"        android:layout_weight="4"        android:layout_height="match_parent"        android:hint="写下你的评论吧..."        android:textSize="@dimen/dimen_sp_13"        android:textColor="@color/black"        android:background="@drawable/inputmethod_edittextbg"        android:padding="@dimen/dimen_dp_5"        android:gravity="center_vertical"        android:layout_marginLeft="@dimen/dimen_dp_5"        android:id="@+id/edit_comment"        />    <TextView        android:layout_width="0dp"        android:layout_weight="1"        android:layout_height="match_parent"        android:textColor="@color/white"        android:text="发送"        android:id="@+id/tv_send"        android:background="#5a718b"        android:gravity="center"        android:textSize="@dimen/dimen_sp_13"        android:layout_marginLeft="@dimen/dimen_dp_10"        android:layout_marginRight="@dimen/dimen_dp_5"        /></LinearLayout>
</RelativeLayout>


阅读全文
0 0
原创粉丝点击