Android 仿网易新闻评论Dialog

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

Android 仿网易新闻评论Dialog_第1张图片
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 布局文件:




    

        

        

        
    

    

    


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)