前言:项目中用到了界面详情评论功能这里小记一下
界面展示
如图 图一红框是一个LinearLayout布局包裹的TextView,当点击请输入的时候 弹出自定义dialog如图二
代码步骤 (这里不再展示activity界面布局)
1.创建dialog布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/beijinghui"
android:orientation="horizontal">
android:id="@+id/ed_comment"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_margin="@dimen/dp_10"
android:layout_weight="1"
android:background="@drawable/hui_kong_bg"
android:hint="请输入您要评论的内容"
android:maxLines="4"
android:padding="@dimen/dp_7"
android:textColor="#434350"
android:textColorHint="#AFAFAF"
android:textSize="@dimen/sp_12" />
android:id="@+id/btn_fabu_pl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:padding="@dimen/dp_10"
android:text="发布"
android:textColor="#434350"
android:textSize="@dimen/sp_14" />
</LinearLayout>
2.点击按钮弹出dialog
/**
* 初始化dialog
*/
private void showDialog() {
Dialog dialog =new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.popu_comment);
Window dialogWindow = dialog.getWindow();
dialogWindow.setGravity(Gravity.BOTTOM);
dialogWindow.getDecorView().setPadding(0, 0, 0, 0);
WindowManager.LayoutParams lp = dialogWindow.getAttributes();
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
dialogWindow.setAttributes(lp);
dialog.show();
//查找控件
EditText edComment = dialog.findViewById(R.id.ed_comment);
TextView btnFaBu = dialog.findViewById(R.id.btn_fabu_pl);
btnFaBu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
//提交之后要讲str清空
str ="";
//展示框复原
tvContent.setText("请输入您的评论内容");
}
});
//判断有没有编辑过评论内容 如果编辑过就在显示出来
if (!TextUtils.isEmpty(str)) {
edComment.setText(str);
edComment.setSelection(str.length());//将光标移至文字末尾
}
//添加监听
edComment.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
str =edComment.getText().toString();
tvContent.setText(str);
}
});
showSoft();
dismissSofo(dialog);
}
3.当点击白色位置时,关闭dialog这个时候也要将键盘关闭
/**
* 关闭软键盘
*
* @param dialog
*/
private void dismissSofo(Dialog dialog) {
//针对dialog隐藏做一个监听 当dialog隐藏的时候 就关闭
dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
InputMethodManager inputMgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputMgr.toggleSoftInput(InputMethodManager.HIDE_NOT_ALWAYS, 0);
}
});
}
4.显示dilog的同时开启键盘
/**
* 开启软键盘
*/
private void showSoft() {
Handler handle =new Handler();
handle.postDelayed(new Runnable() {
@Override
public void run() {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
}
}, 100);
}