简体   繁体   English

如何在对话框打开时自动打开键盘

[英]How to open keyboard automatically when a dialog opens

I am trying to create a dialog with 2 EditText views where one will get the focus and opens up the keyboard automatically. 我正在尝试使用2个EditText视图创建一个对话框,其中一个将获得焦点并自动打开键盘。 However, I can't seem to get it to work. 但是,我似乎无法让它发挥作用。 Below shows my code: 下面显示了我的代码:

public class ItemDetailsDialogFragment extends DialogFragment {
public ItemDetailsDialogFragment() {
    // Required empty public constructor
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    // Using the builder class for convenient dialog construction
    AlertDialog.Builder detailsInput = new AlertDialog.Builder(getActivity());

    // Get the layout inflater
    LayoutInflater inflater = getActivity().getLayoutInflater();

    // Inflate and set the layout for the dialog
    // Pass null as the parent view because its going in the dialog layout
    detailsInput.setView(inflater.inflate(R.layout.fragment_add_item_details, null))
    // Add action buttons
        .setPositiveButton(R.string.details_ok_button, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int id) {
                // TODO
                // Save the details into a map
            }
        })
        .setNegativeButton(R.string.details_cancel_button, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                ItemDetailsDialogFragment.this.getDialog().cancel();
            }
        });
    return detailsInput.create();
}

And here is my xml file: 这是我的xml文件:

<EditText
    style="@style/details_text"
    android:id="@+id/details_desc_input"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

<EditText
    style="@style/details_text"
    android:id="@+id/details_price_input"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="numberDecimal"/>

If I tried to add this line of code in: detailsInput.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE); 如果我尝试在以下位置添加以下代码: detailsInput.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);

I get an error stating 我收到一个错误说明

cannot resolve getWindow() method 无法解析getWindow()方法

I also tried using this suggestion and it works. 我也试过使用这个建议 ,它的确有效。 However, I do not want to re-write that entire block of code everytime I need that dialog. 但是,每次我需要该对话框时,我都不想重写整个代码块。 Instead, I am trying to create a class so I could create an object with the class. 相反,我正在尝试创建一个类,以便我可以使用该类创建一个对象。 I am new to android development so any help would be greatly appreciated! 我是Android开发的新手,所以任何帮助将不胜感激!

What am I doing wrong? 我究竟做错了什么?

If you just want to have focus on one EditText you can do it in 2 ways: 如果您只想关注一个EditText,可以通过两种方式实现:

  • calling mEditText.requestFocus(); 调用mEditText.requestFocus(); in the onCreate method onCreate方法中
  • adding <requestFocus /> in your layout file. 在布局文件中添加<requestFocus />

for example if you want focus on your first EditText (and keyboard automatically opened) your xml should be like that: 例如,如果您想要专注于您的第一个EditText(并且键盘自动打开),您的xml应该是这样的:

<EditText
    style="@style/details_text"
    android:id="@+id/details_desc_input"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <requestFocus />    <!-- here's the trick -->
</EditText>

<EditText
    style="@style/details_text"
    android:id="@+id/details_price_input"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="numberDecimal"/>

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

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