乐趣区

Android类似朋友圈评论视图Dialog实现

很常见的一种成果,点击评论弹出输入框,点空白区域或点“发送”后输入框隐没并收起键盘,通常会遇到键盘遮挡输入框问题,应用 Dialog 的形式解决这个问题绝对简略点,更重要是能代码模块化减少复用性。

dialog_family_dynamic_comment.xml 评论布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/transparent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal"
        android:background="@android:color/white">

        <EditText
            android:id="@+id/et_content"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1" />

        <Button
            android:id="@+id/btn_send"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/btn_send" />
    </LinearLayout>
</RelativeLayout>

FamilyDynamicCommentDialog.java

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.careeach.health.ui.view.FamilyDynamicView;
import java.util.Objects;

/**
 * 家庭动静评论
 */
public class FamilyDynamicCommentDialog extends Dialog {

    private EditText etContent;
    private FamilyDynamicView familyDynamicView;

    public FamilyDynamicCommentDialog(@NonNull Context context, FamilyDynamicView familyDynamicView) {super(context, R.style.DialogFullScreen);
        this.familyDynamicView = familyDynamicView;
        Objects.requireNonNull(getWindow()).setBackgroundDrawable(new ColorDrawable());
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_family_dynamic_comment, null, false);
        etContent = (EditText) view.findViewById(R.id.et_content);
        Button btnSend = (Button) view.findViewById(R.id.btn_send);
        setContentView(view);

        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {dismiss();
            }
        });

        btnSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {String content = etContent.getText().toString().trim();
                if (content.length() == 0) {Toast.makeText(getContext(), getContext().getString(R.string.toast_please_input_comment_content), Toast.LENGTH_SHORT).show();
                    return;
                }
                FamilyDynamicCommentDialog.this.familyDynamicView.clickCommentSend(content);
                dismiss();}
        });
    }

    @Override
    public void dismiss() {etContent.setFocusable(false);
        etContent.setFocusableInTouchMode(false);
        InputMethodManager inputMethodManager = (InputMethodManager) getContext().getSystemService(Activity.INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(etContent.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        super.dismiss();}

    @Override
    public void show() {super.show();
        etContent.setFocusable(true);
        etContent.setFocusableInTouchMode(true);
        etContent.requestFocus();
        etContent.post(new Runnable() {
            @Override
            public void run() {InputMethodManager inputMethodManager = (InputMethodManager) getContext().
                        getSystemService(Context.INPUT_METHOD_SERVICE);
                inputMethodManager.toggleSoftInput(0,
                        InputMethodManager.HIDE_NOT_ALWAYS);
            }
        });
    }
}

FamilyDynamicView 为回调接口,因为应用是 mvp 构造,可依据实据状况回调即可。这里通过 familyDynamicView.clickCommentSend(content) 来回调触发发送性能。

R.style.DialogFullScreen 让 Dialog 全屏。

<style name="DialogFullScreen">
        <item name="android:windowFullscreen">true</item>
        <item name="android:windowNoTitle">true</item>
</style>

应用

FamilyDynamicCommentDialog familyDynamicCommentPopup = new FamilyDynamicCommentDialog(this,this);
familyDynamicCommentPopup.show();
退出移动版