實現(xiàn)評論功能(彈框在軟鍵盤上方)

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<Activity> mActivty;

? ? ? ? private MyHandler(Activity mActivty) {

? ? ? ? ? ? this.mActivty = new WeakReference<Activity>(mActivty);

? ? ? ? }

? ? ? ? @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(), "請輸入內(nèi)容", 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 設(shè)置監(jiān)聽器,在 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:

? ? ? ? ? ? ? ? //發(fā)送

? ? ? ? ? ? ? ? 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);

? ? }

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容