Android 朋友圈 输入框评论效果

点击按钮弹出评论框:

InputMethodManager imm = (InputMethodManager)Context.getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
    editsendLayout.setVisibility(View.VISIBLE);
    edittext.setFocusableInTouchMode(true);
    edittext.requestFocus();

点击隐藏时:

if(editsendLayout.getVisibility() == View.VISIBLE) {
			editsendLayout.setVisibility(View.INVISIBLE);			
                	InputMethodManager imm = (InputMethodManager)mContext.getSystemService(Context.INPUT_METHOD_SERVICE);       
		        imm.hideSoftInputFromWindow(edittext.getWindowToken(), 0);  
						}

实现点击返回键软键盘和输入框一同消失:

重写edittext:

public class Custom_edittext extends EditText {

    public Custom_edittext(Context context) {
        super(context);
    }

    public Custom_edittext(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public Custom_edittext(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public boolean dispatchKeyEventPreIme(KeyEvent event) {
        if (mOnCancelDialogImp != null) {
            mOnCancelDialogImp.onCancelDialog();
        }
      //true表示点击返回键后页面不关闭,false点击返回键后页面也一同关闭
        return true;
    }

    private OnCancelDialogImp mOnCancelDialogImp;

    public void setOnCancelDialogImp(OnCancelDialogImp onCancelDialogImp) {
        mOnCancelDialogImp = onCancelDialogImp;
    }

    /**
     * 在这里定义一个接口 用于在输入框弹出的时候(评论)的时候  点击back按键不响应onKeyDown  和 onKeyPressed 方法
     * 但是查询api  是可以在自定义的view里面走dispatchKeyEventPreIme这个方法的(这个方法的响应在软键盘的响应之前)
     * 所以当按下back的时候,肯定会优先走这个方法  所以在这里写一个回调接口,那么就可以在调用这个回调的时候出发我们需要
     * 响应的逻辑   (比如dialog隐藏)
     */
    public interface OnCancelDialogImp {
        void onCancelDialog();
    }

调用:

 edit_comm.setOnCancelDialogImp(new Custom_edittext.OnCancelDialogImp() {
            @Override
            public void onCancelDialog() {
                if (comm_rl.getVisibility() == View.VISIBLE) {
                    comm_rl.setVisibility(View.GONE);
                    InputMethodManager imm = (InputMethodManager) Lvxingyy_activity.this.getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(edit_comm.getWindowToken(), 0);
                }
            }
        });



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值