简体   繁体   English

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

[英]How to show the keyboard to type in an EditText in an AlertDialog?

I made a custom alertDialog and in it I have put an EditText, but the problem is the keyboard won't show when I click on the EditText to write an input! 我做了一个自定义的alertDialog,并在其中放了一个EditText,但是问题是当我单击EditText编写输入时,键盘不会显示!

Here's my alert dialog class: 这是我的警报对话框类:

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();

            }
        });
    }
}

Please notice that I can't use getSystemService() because it's connected to the activity that started the alertDialog so when I use it like this 请注意,我不能使用getSystemService(),因为它已连接到启动alertDialog的活动,所以当我像这样使用它时

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

the keyboard shows behind the alert dialog and in front of the starting activity 键盘显示在警报对话框的后面和开始活动的前面

For hiding keyboard: 隐藏键盘:

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

For showing keyboard: 显示键盘:

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

我解决了这个问题,而不是使用AlertDialog,我使用了Dialog类

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM