如何显示键盘以在AlertDialog中键入EditText?

问题描述 投票:0回答:2

我做了一个自定义alertDialog并在其中我放了一个EditText,但问题是当我点击EditText写输入时键盘不会显示!

这是我的警告对话框类:

public class EditTaskDialog extends AlertDialog {



  Activity mParent;
    public EditTaskDialog(Context context, Activity parent) {
        super(context);
        mParent = parent;
    }

    @BindView(R.id.btn_edit_edti_task_dialog)
    Button btn_edit_edti_task_dialog;
    @BindView(R.id.tv_time_edit_task_dialog)
    TextView tv_time_edit_task_dialog;
    @BindView(R.id.ll_time_edit_task_dialog)
    LinearLayout ll_time_edit_task_dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //the layout that have the edit text view
        setContentView(R.layout.edit_task_dialog);

        ButterKnife.bind(this);

        final DatePickerDialog.OnDateSetListener x = new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                tv_time_edit_task_dialog.setText(dayOfMonth+ "/" + (month+1) +"/"+ year);
            }
        };

        ll_time_edit_task_dialog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                new DatePickerDialog(
                        mParent,
                        x,
                        Calendar.getInstance().get(Calendar.YEAR),
                        Calendar.getInstance().get(Calendar.MONTH),
                        Calendar.getInstance().get(Calendar.DAY_OF_MONTH)
                ).show();

            }
        });
    }
}

请注意我不能使用getSystemService(),因为它连接到启动alertDialog的活动,所以当我像这样使用它时

InputMethodManager imm = (InputMethodManager)   mParent.getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);

键盘显示在警告对话框后面和启动活动前面

android android-alertdialog
2个回答
0
投票

隐藏键盘:

InputMethodManager imm = (InputMethodManager)getSystemService(
  Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);

用于显示键盘:

InputMethodManager imm = (InputMethodManager)   getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);

0
投票

我修复了问题而不是使用AlertDialog我使用了Dialog类

© www.soinside.com 2019 - 2024. All rights reserved.