基于自定义控件KeyboardDetectorRelativeLayout,实现评论功能

项目需求,要求实现评论功能,查了好久,发现基于这个控件进行实现较为简单.

闲话不多说,首先activity的输入法模式设置为

android:windowSoftInputMode="stateHidden|adjustResize"

效果图如下:



还是先把巨人的肩膀放出来,

/**
 * Created by GerryRun on 2017/12/28 0028.
 */

public class KeyboardDetectorRelativeLayout extends RelativeLayout{

    private OnSoftKeyboardListener listener;

    public KeyboardDetectorRelativeLayout(Context context) {
        this(context, null);
        // TODO Auto-generated constructor stub
    }

    public KeyboardDetectorRelativeLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public KeyboardDetectorRelativeLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    //获取输入法的高度主要在此方法中执行,当界面布局发生变化时,会执行此方法,从而获取到输入法的高度
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // TODO Auto-generated method stub
        final int proposedHeight = View.MeasureSpec.getSize(heightMeasureSpec);
        final int actualHeight = getHeight();
        int diff = actualHeight - proposedHeight;
        Log.i("TAG", "proposedHeight=" + proposedHeight);
        Log.i("TAG", "actualHeight=" + actualHeight);
        Log.i("TAG", "diff=" + diff);
        if (diff > 100) {
            Log.i("TAG", KLApplication.getInstance().getApplicationContext().toString());
            SharedPreferences sh = KLApplication.getInstance().getApplicationContext().getSharedPreferences("in_put", Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = sh.edit();
            editor.putInt("size", diff);
            Log.i("TAG", "size=" + diff);
            editor.commit();
            if (null != listener) {
                listener.onShown(diff);
            }

        } else if (diff < -100) {
            if (null != listener) {
                listener.onHidden();
            }
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        if (null != listener) {
            listener.onMeasureFinished();
        }


    }

    public void setOnSoftKeyboardListener(OnSoftKeyboardListener listener) {
        this.listener = listener;
    }

    //输入法面板弹出。隐藏以及布局测量完毕执行的方法
    public interface OnSoftKeyboardListener {
        void onShown(int keyboardHeight);

        void onHidden();

        void onMeasureFinished();
    }
}

再拿 KeyboardDetectorRelativeLayout作为父布局,正常流程findViewById走完之后,再实现自定义View里的接口
//输入法面板弹出。隐藏以及布局测量完毕执行的方法
public interface OnSoftKeyboardListener {
    void onShown(int keyboardHeight);

    void onHidden();

    void onMeasureFinished();
}
实现接口方法如下,直接在相应方法:
 
 
private KeyboardDetectorRelativeLayout.OnSoftKeyboardListener mOnSoftKeyboardListener = new KeyboardDetectorRelativeLayout.OnSoftKeyboardListener() {
    @Override
    public void onShown(int keyboardHeight) {
        //输入法显示的时候,调用此方法,如显示输入框等逻辑
        scroll_no_use_pinglun_list.setVisibility(View.VISIBLE);
        rela_edit_text_pinglun_input.setVisibility(View.VISIBLE);
        edit_text_pinglun_input.requestFocus();

        if (scrollLayoutParams == null) {
            scrollLayoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            scroll_no_use_pinglun_list.setLayoutParams(scrollLayoutParams);
        }

        if (marginLayoutParams == null) {
            marginLayoutParams = new RelativeLayout.LayoutParams(rela_edit_text_pinglun_input.getLayoutParams());
            marginLayoutParams.setMargins(marginLayoutParams.leftMargin, (int) (KLApplication.getHight() - KLApplication.dp2px(70)
                    - marginLayoutParams.topMargin - keyboardHeight), marginLayoutParams.rightMargin, marginLayoutParams.bottomMargin);

            layoutParams = new RelativeLayout.LayoutParams(marginLayoutParams);
            rela_edit_text_pinglun_input.setLayoutParams(layoutParams);
        }

        edit_text_pinglun_input.setHint("评论:" + mList.get(mClickPosition).nickName);
    }

    @Override
    public void onHidden() {
        //输入法隐藏时调用
        scroll_no_use_pinglun_list.setVisibility(View.GONE);
        rela_edit_text_pinglun_input.setVisibility(View.GONE);
    }

    @Override
    public void onMeasureFinished() {

    }
};

猜你喜欢

转载自blog.csdn.net/gerryrun/article/details/79132132