仿朋友圈评论弹出键盘,同时在键盘上弹出输入框

    /**
    * 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);
   }
}

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值