InputUtil.java
5.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
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);
}
}