Android 仿网易新闻评论Dialog

作者: 落花有意而流水无情 | 来源:发表于2016-11-03 09:46 被阅读1304次

    项目中有个新闻模块,需要一个评论功能,产品也没设计,小公司么!不容易啊,看了下网易新闻,感觉是一个弹出的dialog,自动打开软键盘。很简单,自己动手实现了一下。。。

    Paste_Image.png

    1,自定义一个CommentDialog,对dialog弹出时进行监听,并且打开软键盘,这时布局就会被推上去,在输入法的头部显示,代码如下:

              setOnShowListener(new OnShowListener() {
    
                @Override
                public void onShow(DialogInterface dialog) {
                    mEdittext.setFocusableInTouchMode(true);
                    mEdittext.requestFocus();
                    InputMethodManager inputManager = (InputMethodManager) mEdittext
                            .getContext().getSystemService(
                                    Context.INPUT_METHOD_SERVICE);
                    inputManager.showSoftInput(mEdittext,
                            InputMethodManager.SHOW_IMPLICIT);
                }
            });
    

    2, dialog中封装一个接口,对外进行Edittext输入内容的回调,用户自己去处理提交评论事件:

    public interface OnSendListener {
         void sendComment(String content);
     }
    

    3 布局文件:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/gray"
        android:orientation="vertical" >
    
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="5dp" >
    
            <TextView
                android:id="@+id/tv_cancel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="取消"
                android:textColor="@color/white" />
    
            <TextView
                android:id="@+id/tv_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:text="写评论"
                android:textColor="@color/white" />
    
            <TextView
                android:id="@+id/tv_send"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:text="发送"
                android:textColor="@color/white" />
        </RelativeLayout>
    
        <View
            android:layout_width="match_parent"
            android:layout_height="1px"
            android:layout_marginTop="6dp"
            android:background="#cccccc" />
    
        <EditText
            android:id="@+id/et_comment"
            android:layout_width="match_parent"
            android:layout_height="80dp"
            android:layout_marginLeft="3dp"
            android:background="@drawable/bg_edittext"
            android:gravity="top|left"
            android:hint="请输入评论内容(最多为50字)"
            android:maxLength="50"
            android:minLines="3"
            android:textColor="#000000"
            android:textColorHint="#696969"
            android:textSize="13sp" 
            />
    
    </LinearLayout>
    

    4 CommentDialog.java 文件

    package com.zhiyu.tugou.view.dialog;
    
    import android.app.Dialog;
    import android.content.Context;
    import android.content.DialogInterface;
    import android.os.Bundle;
    import android.text.TextUtils;
    import android.view.Display;
    import android.view.Gravity;
    import android.view.View;
    import android.view.ViewGroup;
    import android.view.Window;
    import android.view.WindowManager;
    import android.view.inputmethod.InputMethodManager;
    import android.widget.EditText;
    import android.widget.TextView;
    
    import com.zhiyu.tugou.AppContext;
    import com.zhiyu.tugou.R;
    import com.zhiyu.tugou.utils.UIHelper;
    
    /**
     * 评论
     * 
     * @author zhenghc
     * onSendListeneronSendListener
     */
    public class CommentDialog extends Dialog implements
            android.view.View.OnClickListener {
    
        public interface OnSendListener {
            void sendComment(String content);
        }
    
        private Context mContext;
        private EditText mEdittext;
        private TextView mTvCancel;
        private TextView mTvSend;
        private OnSendListener onSendListener;
    
        public void setOnSendListener(OnSendListener onSendListener) {
            this.onSendListener = onSendListener;
        }
    
        public CommentDialog(Context context) {
            super(context,R.style.Dialog_Transparent);
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            this.mContext = context;
        }
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            View v = (ViewGroup) View.inflate(mContext,
                    R.layout.comment_edittext_dialoglayout, null);
    
            initView(v);
            setContentView(v);
            setLayout();
            setOnShowListener(new OnShowListener() {
    
                @Override
                public void onShow(DialogInterface dialog) {
                    mEdittext.setFocusableInTouchMode(true);
                    mEdittext.requestFocus();
                    InputMethodManager inputManager = (InputMethodManager) mEdittext
                            .getContext().getSystemService(
                                    Context.INPUT_METHOD_SERVICE);
                    inputManager.showSoftInput(mEdittext,
                            InputMethodManager.SHOW_IMPLICIT);
                }
            });
        }
    
        private void initView(View v) {
            mEdittext = (EditText) v.findViewById(R.id.et_comment);
            mTvCancel = (TextView) v.findViewById(R.id.tv_cancel);
            mTvSend = (TextView) v.findViewById(R.id.tv_send);
            mTvCancel.setOnClickListener(this);
            mTvSend.setOnClickListener(this);
        }
    
        private void setLayout() {
            getWindow().setGravity(Gravity.BOTTOM);
            WindowManager m = getWindow().getWindowManager();
            Display d = m.getDefaultDisplay();
            WindowManager.LayoutParams p = getWindow().getAttributes();
            p.width = WindowManager.LayoutParams.MATCH_PARENT;
            p.height = WindowManager.LayoutParams.WRAP_CONTENT;
            getWindow().setAttributes(p);
        }
    
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
            case R.id.tv_cancel:
                dismiss();
                break;
            case R.id.tv_send:
                String content = mEdittext.getText().toString().trim();
                if (TextUtils.isEmpty(content)) {
                    UIHelper.ToastMessage(AppContext.getInstance(), "请输入评论");
                    return;
                }
                
                if (onSendListener != null) {
                    onSendListener.sendComment(content);
                }
                break;
            default:
                break;
            }
        }
    }
    
    

    相关文章

      网友评论

        本文标题:Android 仿网易新闻评论Dialog

        本文链接:https://www.haomeiwen.com/subject/oiubuttx.html