类朋友圈评论视图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.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>
 

使用Dialog

FamilyDynamicCommentDialog familyDynamicCommentPopup = new FamilyDynamicCommentDialog(this,this);familyDynamicCommentPopup.show();

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念
xml文件介绍及使用
xml编程(一)-xml语法
XML文件结构和基本语法
第2章 包装类
XML入门的常见问题(二)
Java对象的强、软、弱和虚引用
JS解析XML文件和XML字符串详解
java中枚举的详细使用介绍
了解Xml格式
XML入门的常见问题(四)
深入SQLite多线程的使用总结详解
PlayFramework完整实现一个APP(一)
XML和YAML的使用方法
XML轻松学习总节篇