/**
* show comment popupwindow(弹出评论的popupWindow)
*/
private void showPopupCommnet() {
final View view = LayoutInflater.from(context).inflate(
R.layout.comment_popupwindow, null);
inputComment = (EditText) view
.findViewById(R.id.et_discuss);
btn_submit = (TextView) view.findViewById(R.id.tv_confirm);
popupWindow = new PopupWindow(view, LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT, false);
popupWindow.setTouchable(true);
popupWindow.setTouchInterceptor(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_OUTSIDE)
popupWindow.dismiss();
return false;
}
});
//
popupWindow.setFocusable(true);
// 设置点击窗口外边窗口消失
popupWindow.setOutsideTouchable(true);
// popupWindow.setBackgroundDrawable(getResources().getDrawable(
// R.drawable.popuwindow_white_bg));
// 设置弹出窗体需要软键盘
popupWindow.setSoftInputMode(PopupWindow.INPUT_METHOD_NEEDED);
// 再设置模式,和Activity的一样,覆盖,调整大小。
popupWindow
.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
ColorDrawable cd = new ColorDrawable(0x000000);
popupWindow.setBackgroundDrawable(cd);
popupWindow.showAtLocation(view, Gravity.BOTTOM, 0, 0);
// WindowManager.LayoutParams params = getWindow().getAttributes();
// params.alpha = 0.4f;
// getWindow().setAttributes(params);
// 设置popWindow的显示和消失动画
// popupWindow.setAnimationStyle(R.style.mypopwindow_anim_style);
popupWindow.update();
popupInputMethodWindow();
popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
// 在dismiss中恢复透明度
public void onDismiss() {
// WindowManager.LayoutParams params = getWindow().getAttributes();
// params.alpha = 1f;
// getWindow().setAttributes(params);
}
});
btn_submit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// btn_submit.setClickable(false);
String comment1 = inputComment.getText().toString().trim();
Log.i("comment1--------", comment1);
if (comment1 == null || "".equals(comment1)) {
Toast.makeText(DiscoverDetailsActivity.this, "请输入评论内容", Toast.LENGTH_SHORT).show();
return;
}
//调用提交评论接口
saveDiscuss(comment1);
popupWindow.dismiss();
}
});
}
comment_popupwindow布局文件:
<?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="44dp"
android:background="#ffffff"
android:gravity="center_vertical"
android:orientation="horizontal" >
<EditText
android:id="@+id/et_discuss"
android:layout_width="0dp"
android:layout_height="32dp"
android:layout_weight="1"
android:hint="说点什么"
android:textColorHint="#a2a2a2"
android:textSize="13sp"
android:background="@drawable/et_say"
android:layout_marginRight="7dp"
android:layout_marginLeft="15dp"
android:layout_marginBottom="6dp"
android:layout_marginTop="6dp"
android:ellipsize="end"
android:layout_centerVertical="true"
android:paddingLeft="@dimen/ui_10_dip"
android:singleLine="true"
/>
<TextView
android:id="@+id/tv_confirm"
android:text="提交"
android:textSize="13sp"
android:textColorHint="#b7b7b7"
android:textColor="#646464"
android:layout_marginRight="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
</TextView>
</LinearLayout>
此处需要注意的是,当点击返回的时候,键盘先消失掉,但是输入框还在,且需要再点击返回键才会让输入框消失
所以要重写onKeyDown方法
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
System.out.println("按下了back键 onKeyDown()");
finish();
return true;
}else {
return super.onKeyDown(keyCode, event);
}
}