InputUtil.java 5.03 KB
package com.cnlive.gundam.user.util;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import com.cnlive.gundam.R;
import com.cnlive.gundam.video.model.CNMessageModle;
import com.cnlive.gundam.video.model.MessageInfo;
import com.google.gson.Gson;

/**
 * Created by xiansong on 2017/4/12.
 */

public class InputUtil {

    private PopupWindow mInputPopupWindow;

    private Context mContext;
    private EditText mMsgEditText;

    private SendCallback mSendCallback;

    private Gson mGson;
    public InputUtil(Context context, SendCallback callback) {
        mContext = context;
        mGson = new Gson();
        mSendCallback = callback;
    }

    public void dismiss() {
        if (mInputPopupWindow != null) {
            mInputPopupWindow.dismiss();
            mInputPopupWindow = null;
        }
    }

    public void showInput(View view) {
        // 加载popupWindow的布局
        View contentView = LayoutInflater.from(mContext).inflate(R.layout.layout_chat_input, new LinearLayout(mContext), false);
        contentView.findViewById(R.id.click_view).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mMsgEditText.setText("");
                mInputPopupWindow.dismiss();
            }
        });
        mMsgEditText = (EditText) contentView.findViewById(R.id.chat_content);
        mMsgEditText.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {
                    InputMethodManager inputMethodManager = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
                    if (inputMethodManager.isActive()) {
                        inputMethodManager.hideSoftInputFromWindow(v.getApplicationWindowToken(), 0);
                    }
                    sendMessage();
                    return true;
                }
                return false;
            }
        });
        /*contentView.findViewById(R.id.post_button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mMsgEditText.setText("");
                mInputPopupWindow.dismiss();
            }
        });*/

        mInputPopupWindow = new PopupWindow(contentView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        mInputPopupWindow.setTouchable(true);
        mInputPopupWindow.setTouchInterceptor(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_OUTSIDE)
                    mInputPopupWindow.dismiss();
                return false;
            }
        });
        // 设置以下两个属性后 点击popupWindow之外的地方 就会让其消失
        // 设置popupWindow 点击popupWindow外的地方 生效
        mInputPopupWindow.setOutsideTouchable(true);
        // 设置popupWindow 背景
        mInputPopupWindow.setBackgroundDrawable(new BitmapDrawable());
        mInputPopupWindow.setFocusable(true); //设置获取焦点
        mInputPopupWindow.setAnimationStyle(R.style.animation);
        mInputPopupWindow.setSoftInputMode(PopupWindow.INPUT_METHOD_NEEDED);
        mInputPopupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        mInputPopupWindow.showAtLocation(view, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
        mInputPopupWindow.update();

        mMsgEditText.requestFocus();
        mInputPopupWindow.setSoftInputMode(PopupWindow.INPUT_METHOD_NEEDED);
        mInputPopupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        InputMethodManager imm = (InputMethodManager) mContext.getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.toggleSoftInput(1000, InputMethodManager.HIDE_NOT_ALWAYS);
    }


    public void sendMessage() {
        //发送聊天室消息
        if (TextUtils.isEmpty(mMsgEditText.getText())) {
            ToastUtil.showToast(mContext, "不能发送空消息呦~");
        } else {
            if(mSendCallback!=null) mSendCallback.sendMessage(mGson.toJson(new CNMessageModle("chatRoom",new MessageInfo("","","","",mMsgEditText.getText().toString()))));
            mMsgEditText.setText("");
        }
        if (mInputPopupWindow != null && mInputPopupWindow.isShowing()) mInputPopupWindow.dismiss();
    }




    public interface SendCallback {

        void sendMessage(String message);
    }



}