public class VideoSubCommentDialogFragment extends DialogFragment implements View.OnClickListener {
EditText commentEditText;
TextView tvSend;
InputMethodManager inputMethodManager;
CommentClickListener mCommentClickListener;
DialogFragmentDataCallback dataCallback;
String mPreCommentText;
String mHint;
LinearLayout llComment;
Handler mHandler;
private Rect mRect = new Rect();
private boolean softKeyBoardIsVisible;
Dialog mDialog;
int count = 0;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
mDialog = new Dialog(getActivity(), R.style.BottomDialog);
mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
mDialog.setContentView(R.layout.dialog_fragment_comment_layout);
mDialog.setCanceledOnTouchOutside(true);
Window window = mDialog.getWindow();
WindowManager.LayoutParams layoutParams;
if (window != null) {
layoutParams = window.getAttributes();
layoutParams.gravity = Gravity.BOTTOM;
layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
window.setAttributes(layoutParams);
}
commentEditText = (EditText) mDialog.findViewById(R.id.edt_comment);
tvSend = (TextView) mDialog.findViewById(R.id.tv_send);
llComment = (LinearLayout) mDialog.findViewById(R.id.ll_comment);
tvSend.setOnClickListener(this);
mHandler = new MyHandler(getActivity());
initText();
initListener();
setSoftKeyboard();
// fillEditText();
return mDialog;
}
private void initText() {
if (getArguments() != null) {
mPreCommentText = getArguments().getString("text");
mHint = getArguments().getString("hint");
if (!TextUtils.isEmpty(mPreCommentText)) {
commentEditText.setText(mPreCommentText);
} else {
commentEditText.setText("");
}
if (!TextUtils.isEmpty(mHint)) {
commentEditText.setHint(mHint);
} else {
commentEditText.setText("");
}
}
}
@Override
public void onStop() {
super.onStop();
dismissAllowingStateLoss();
if (mHandler != null) {
mHandler.removeMessages(1);
mHandler = null;
}
}
public class MyHandler extends Handler {
private final WeakReference
private MyHandler(Activity mActivty) {
this.mActivty = new WeakReference
}
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
Activity activity = mActivty.get();
if (activity != null) {
if (activity.isFinishing()) {
return;
}
switch (msg.what) {
case 1:
int[] location = new int[2];
llComment.getLocationOnScreen(location);
int height = location[1];
int screenHeight = SysUtils.getScreenSize(getActivity())[1];
if (height > screenHeight - 200) {
count++;
if (count > 1) {
//键盘缩起
if (mDialog != null) {
mDialog.dismiss();
}
return;
}
}
mHandler.sendEmptyMessageDelayed(1, 200);
break;
default:
break;
}
}
}
}
@Override
public void onStart() {
super.onStart();
Window window = getDialog().getWindow();
WindowManager.LayoutParams windowParams = window.getAttributes();
windowParams.dimAmount = 0.0f;
window.setAttributes(windowParams);
}
private void initListener() {
commentEditText.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View view, int keyCode, KeyEvent keyEvent) {
if (keyCode == KeyEvent.KEYCODE_ENTER && keyEvent.getAction() == KeyEvent.ACTION_DOWN) {
//评论
sendComment(view);
return true;
} else if (keyCode == KeyEvent.KEYCODE_BACK && keyEvent.getAction() == KeyEvent.ACTION_DOWN) {
dismiss();
}
return false;
}
});
}
private void sendComment(View view) {
String commentContent = commentEditText.getText().toString().trim();
// String commentContent = stringToUtf8(mEdtComment.getText().toString().trim());
if (TextUtils.isEmpty(commentContent)) {
Toast.makeText(getActivity(), "请输入内容", Toast.LENGTH_SHORT).show();
} else {
InputMethodManager imm = (InputMethodManager) view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm.isActive(view)) {
imm.hideSoftInputFromWindow(view.getApplicationWindowToken(), 0);
}
if (mCommentClickListener != null) {
mCommentClickListener.toComment(commentContent);
}
dismiss();
}
}
interface CommentClickListener {
void toComment(String comment);
String getCommentText();
void setCommentText(String commentTextTemp);
}
public void setCommentClickListener(CommentClickListener commentClickListener) {
this.mCommentClickListener = commentClickListener;
}
private void setSoftKeyboard() {
commentEditText.setFocusable(true);
commentEditText.setFocusableInTouchMode(true);
commentEditText.requestFocus();
//为 commentEditText 设置监听器,在 DialogFragment 绘制完后立即呼出软键盘,呼出成功后即注销
commentEditText.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
inputMethodManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
if (inputMethodManager != null) {
if (inputMethodManager.showSoftInput(commentEditText, 0)) {
commentEditText.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
}
if (mHandler != null) {
mHandler.sendEmptyMessageDelayed(1, 200);
}
}
});
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.tv_send:
//发送
sendComment(commentEditText);
break;
default:
break;
}
}
@Override
public void onDismiss(DialogInterface dialog) {
if (mCommentClickListener != null) {
mCommentClickListener.setCommentText(commentEditText.getText().toString());
}
count = 0;
super.onDismiss(dialog);
}
@Override
public void onCancel(DialogInterface dialog) {
if (mCommentClickListener != null) {
mCommentClickListener.setCommentText(commentEditText.getText().toString());
}
count = 0;
super.onCancel(dialog);
}
}