为什么我使用softInputMode并在键盘消失时不返回原始位置?

时间:2018-08-30 10:53:58

标签: android

我正在将输入框界面添加到WindowManager。我已经在softInputMode中添加了LayoutParams。当我专注于EditText时,键盘会跳出来并达到我想要的效果!但是当键盘消失时,我的布局不会恢复到原始位置。

在Android 7.0界面中可以恢复到原始状态,但将在Android 6.0中停止

In Android 7.0 In Android 6.0

我正在使用一个程序来创建布局。 这是我的代码。

public class TestCreateViewActivity extends BaseActivity {

@BindView(R.id.btn_show) Button mBtnShow;

private WindowManager mManager;
private WindowManager.LayoutParams mParamsContent;
private int mScreenWidth, mScreenHeight;
private FrameLayout mContent;
private int mOffsetHeight, mTopHeight, mBottomHeight;

@SuppressLint("CheckResult")
@Override
protected void initial(Bundle savedInstanceState) {

    initWindow();
    initializeContent();

    onClicks(mBtnShow).subscribe(v -> mContent.setVisibility(View.VISIBLE));
}

@Override
public void onDestroy() {
    super.onDestroy();

    mManager.removeView(mContent);
}

@Override
protected int onResource() {
    return R.layout.activity_test_create_view;
}


private void initWindow() {
    mManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);

    if (mManager != null) {
        Display display = mManager.getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        mScreenWidth = size.x;
        mScreenHeight = size.y;
    }

    mOffsetHeight = (int) (mScreenHeight / 6.5) + ViewTools.getStatusBarHeight(mContext);

    mTopHeight = (mScreenHeight - mOffsetHeight) / 7;
    mBottomHeight = mScreenHeight - mOffsetHeight - mTopHeight;


    mParamsContent = new WindowManager.LayoutParams();
    mParamsContent.softInputMode = WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN;

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        mParamsContent.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
    } else if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
        mParamsContent.type = WindowManager.LayoutParams.TYPE_PHONE;
    } else {
        mParamsContent.type = WindowManager.LayoutParams.TYPE_TOAST;
    }

    mParamsContent.flags =
            WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
                    | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
    mParamsContent.format = PixelFormat.RGBA_8888;
    mParamsContent.gravity = Gravity.START | Gravity.TOP;

    mParamsContent.width = mScreenWidth;
    mParamsContent.height = mScreenHeight;
    mParamsContent.windowAnimations = R.style.DialogAnimation;
}

@SuppressLint({"ClickableViewAccessibility", "RtlHardcoded", "CheckResult"})
private void initializeContent() {
    mContent = new FrameLayout(mContext);

    mContent.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));
    mContent.setVisibility(View.GONE);
    mManager.addView(mContent, mParamsContent);

    OverLayContent overLayContent = new OverLayContent(mContext);
    mContent.addView(overLayContent.getView());
}

public class OverLayContent {

    private Context mContext;
    private View mView;
    private LinearLayout mLin;

    public OverLayContent(Context context) {
        this.mContext = context;

        initView();
    }

    private void initView() {
        mLin = new LinearLayout(mContext);
        mLin.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        mLin.setOrientation(LinearLayout.VERTICAL);

        View empthView = new FrameLayout(mContext);
        empthView.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, mOffsetHeight));
        empthView.setBackgroundResource(R.drawable.overlay_top_style);
        mLin.addView(empthView);

        LinearLayout flMain = new LinearLayout(mContext);
        flMain.setBackgroundColor(Color.WHITE);
        flMain.setOrientation(LinearLayout.VERTICAL);
        flMain.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, mTopHeight + mBottomHeight));
        flMain.addView(createTopView());
        flMain.addView(createBottomView());

        mLin.addView(flMain);

        mView = mLin;
    }

    private View createTopView() {
        LinearLayout topView = new LinearLayout(mContext);
        topView.setOrientation(LinearLayout.HORIZONTAL);
        topView.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, mTopHeight));
        topView.setPadding(15, 5, 15, 5);

        View view = LayoutInflater.from(mContext).inflate(R.layout.overlay_circle, null);
        view.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, mTopHeight));
        view.setMinimumWidth(mTopHeight);

        topView.addView(view);

        ((LinearLayout.LayoutParams) view.getLayoutParams()).weight = 1;
        return topView;
    }

    private View createBottomView() {
        FrameLayout bottomView = new FrameLayout(mContext);
        bottomView.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, mBottomHeight));

        LinearLayout linearLayout_main = new LinearLayout(mContext);
        linearLayout_main.setOrientation(LinearLayout.VERTICAL);

        LinearLayout linearLayout_edit = new LinearLayout(mContext);
        LinearLayout.LayoutParams params_edit = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        params_edit.weight = 1;
        linearLayout_edit.setLayoutParams(params_edit);
        linearLayout_edit.setOrientation(LinearLayout.VERTICAL);
        TextView textView = new TextView(mContext);
        LinearLayout.LayoutParams params_text = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        params_text.weight = 1;
        textView.setLayoutParams(params_text);
        textView.setGravity(Gravity.CENTER);
        textView.setText("Input");
        EditText editText = new EditText(mContext);
        LinearLayout.LayoutParams params_editText = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        params_editText.weight = 1;
        editText.setLayoutParams(params_editText);
        linearLayout_edit.addView(textView);
        linearLayout_edit.addView(editText);
        linearLayout_main.addView(linearLayout_edit);

        LinearLayout linearLayout_button = new LinearLayout(mContext);
        LinearLayout.LayoutParams params_button = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        params_button.weight = 1;
        linearLayout_button.setLayoutParams(params_button);
        linearLayout_button.setOrientation(LinearLayout.HORIZONTAL);
        Button button_left = new Button(mContext);
        LinearLayout.LayoutParams params_left = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        params_left.weight = 1;
        button_left.setLayoutParams(params_left);
        button_left.setText("Back");
        linearLayout_button.addView(button_left);
        Button button_right = new Button(mContext);
        LinearLayout.LayoutParams params_right = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        params_right.weight = 1;
        button_right.setLayoutParams(params_right);
        button_right.setText("Back");
        linearLayout_button.addView(button_right);
        linearLayout_main.addView(linearLayout_button);

        bottomView.addView(linearLayout_main);
        return bottomView;
    }

    public View getView() {
        return mView;
    }
}

}

我有什么想念的吗?

0 个答案:

没有答案