前言:最初实现评论的时候,我是在利用setVisibility来控制输入框的隐藏与显示来实现评论。但是发现这样做,弹出键盘的时候会把整个界面推上去,不大符合人们的使用习惯。因此,我决定采用弹窗的形式来实现评论与回复。
记录下用到的场景:1、点击评论ICON,2、点击评论列表进行回复
public class PlDialog extends DialogFragment { //点击发表,内容不为空时的回调 public SendBackListener sendBackListener; public interface SendBackListener{ void sendBack(String inputText); } private String texthint; private Dialog dialog; private EditText inputDlg; private int numconut=300; private String tag=null; public PlDialog() { } @SuppressLint("ValidFragment") public PlDialog(String texthint, SendBackListener sendBackListener){//提示文字 this.texthint=texthint; this.sendBackListener=sendBackListener; } public Dialog onCreateDialog(Bundle savedInstanceState) { // 使用不带Theme的构造器, 获得的dialog边框距离屏幕仍有几毫米的缝隙。 dialog = new Dialog(getActivity(), R.style.PlDialog); dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); // 设置Content前设定 // getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN); View contentview = View.inflate(getActivity(), R.layout.comment_dialog_layout, null); dialog.setContentView(contentview); dialog.setCanceledOnTouchOutside(true); // 外部点击取消 // 设置宽度为屏宽, 靠近屏幕底部。 Window window = dialog.getWindow(); WindowManager.LayoutParams lp = window.getAttributes(); lp.gravity = Gravity.BOTTOM; // 紧贴底部 lp.alpha = 1; lp.dimAmount = 0.5f; lp.width = WindowManager.LayoutParams.MATCH_PARENT; // 宽度持平 window.setAttributes(lp); window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND); inputDlg = (EditText) contentview.findViewById(R.id.dialog_comment_content); inputDlg.setHint(texthint); final TextView tv_send = (TextView) contentview.findViewById(R.id.dialog_comment_send); inputDlg.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { } @Override public void afterTextChanged(Editable s) { if (s.length() > 0) { tv_send.setBackgroundResource(R.drawable.corners_review_cansend); } else { tv_send.setBackgroundResource(R.drawable.corners_review_send); } } }); tv_send.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (TextUtils.isEmpty(inputDlg.getText().toString())) { Toast.makeText(getActivity(),"输入内容为空",Toast.LENGTH_LONG).show(); return; } else { sendBackListener.sendBack(inputDlg.getText().toString()); } } }); inputDlg.setFocusable(true); inputDlg.setFocusableInTouchMode(true); inputDlg.requestFocus(); final Handler hanler = new Handler(); dialog.setOnDismissListener(new DialogInterface.OnDismissListener() { public InputMethodManager mInputMethodManager; @Override public void onDismiss(DialogInterface dialog) { hanler.postDelayed(new Runnable() { @Override public void run() { hideSoftkeyboard(); } }, 200); } }); return dialog; } public void hideSoftkeyboard() { try { ((InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE)) .hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); } catch (NullPointerException e) { } } }
上面的代码是弹出框的代码,如何使用呢?
dialog = new PlDialog("评论:", new PlDialog.SendBackListener() { @Override public void sendBack(final String inputText) { //Toast.makeText(MyApplication.getContextObject(),inputText,Toast.LENGTH_SHORT).show(); final String[] s = pinStr.split("#"); //Toast.makeText(MyApplication.getContextObject(),pinStr,Toast.LENGTH_SHORT).show(); pinglun("pl",s,mData.get(position1).getDy_id()+"",inputText); dialog.dismiss(); } });
dialog.show(getFragmentManager(), "dialog");
如果是回复的话,就修改上段代码中的“评论:”即可。