键盘在片段中打开时,警报对话框不会向上移动

时间:2018-03-27 05:35:49

标签: android android-layout

我已经在片段中动态创建了一个警告对话框。它有一个编辑文本和一个按钮。我的问题是当用户专注于编辑文本键盘打开时,它的隐藏对话框按钮。我的对话框没有移到顶部。

private void AlertEditMode(final MyIshVo myIshVo) {
    final LinearLayout linearLayout=new LinearLayout(getContext());
    final EditText edittext = new EditText(getContext());
    LinearLayout.LayoutParams layoutParams =new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
    layoutParams.setMargins(50,0,50,0);
    edittext.setLayoutParams(layoutParams);
    linearLayout.addView(edittext);
    final AlertDialog dialog = new AlertDialog.Builder(getContext())
            .setView(linearLayout)
            .setTitle("Instant Share")
            .setPositiveButton(android.R.string.ok, null) //Set to null. We override the onclick
            .setNegativeButton(android.R.string.cancel, null)
            .create();


    // relativeLayout.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

    edittext.setText(myIshVo.getMish_name());

    edittext.setSelection(edittext.getText().length());
    dialog.setOnShowListener(new DialogInterface.OnShowListener() {

        @Override
        public void onShow(final DialogInterface dialog) {

            Button button = ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_POSITIVE);
            button.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View view) {
                    String ish_title = edittext.getText().toString().trim();
                    String instant_share_Id=myIshVo.getMinstant_share_Id();
                    if(ish_title==null || ish_title.equalsIgnoreCase("") || ish_title.contains("<") || ish_title.contains("\\") ){
                        //showToastMessage("Title should not be Empty");
                        edittext.setError("Please enter valid title.");
                    }else{
                        presenter.setEditIsh(ish_title,instant_share_Id);
                        dialog.dismiss();
                    }
                }
            });
        }
    });
    dialog.show();
   // dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

}

我已经尝试将adjustresize赋予活动并动态提供

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

但两种解决方案都无法正常工作。其他任何解决方案请提前表示感谢。

[![查看图片] [1]] [1]

2 个答案:

答案 0 :(得分:7)

当显示Activity的{​​{1}}具有半透明状态栏主题时,我遇到了此问题。

因此,我没有使用使用上下文主题的经典AlertDialog初始化构建器,而是明确地传递了一个没有半透明状态栏的对话框主题:

AlertDialog.Builder(context)

我选择了这个主题,因为它确保了与棒棒糖前设备的兼容性,但AlertDialog.Builder(context, android.R.style.Theme_DeviceDefault_Light_Dialog_NoActionBar) 之类的其他设备也可以解决这个问题。

答案 1 :(得分:1)

如果你想在顶部显示对话框,那么在代码下面使用..

    private void AlertEditMode(final MyIshVo myIshVo) {
    final LinearLayout linearLayout=new LinearLayout(getContext());
    final EditText edittext = new EditText(getContext());
    LinearLayout.LayoutParams layoutParams =new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
    layoutParams.setMargins(50,0,50,0);
    edittext.setLayoutParams(layoutParams);
    linearLayout.addView(edittext);
    final AlertDialog dialog = new AlertDialog.Builder(getContext())
            .setView(linearLayout)
            .setTitle("Instant Share")
            .setPositiveButton(android.R.string.ok, null) //Set to null. We override the onclick
            .setNegativeButton(android.R.string.cancel, null)
            .create();


    // relativeLayout.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

    edittext.setText(myIshVo.getMish_name());

    edittext.setSelection(edittext.getText().length());
    dialog.setOnShowListener(new DialogInterface.OnShowListener() {

        @Override
        public void onShow(final DialogInterface dialog) {

            Button button = ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_POSITIVE);
            button.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View view) {
                    String ish_title = edittext.getText().toString().trim();
                    String instant_share_Id=myIshVo.getMinstant_share_Id();
                    if(ish_title==null || ish_title.equalsIgnoreCase("") || ish_title.contains("<") || ish_title.contains("\\") ){
                        //showToastMessage("Title should not be Empty");
                        edittext.setError("Please enter valid title.");
                    }else{
                        presenter.setEditIsh(ish_title,instant_share_Id);
                        dialog.dismiss();
                    }
                }
            });
        }
    });
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    WindowManager.LayoutParams wmlp = dialog.getWindow().getAttributes();

    wmlp.gravity = Gravity.TOP | Gravity.LEFT;
    wmlp.x = 100;   //x position
    wmlp.y = 100;   //y position

    dialog.show();
    // dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

}