在DialogFragment中使用适配器时,软键盘不显示

boo*_*ark 3 android android-arrayadapter android-dialogfragment

我有一个自定义的DialogFragment,里面有一个ArrayAdapter,里面有一些editTexts.显示对话框时,即使我按下编辑文本,也不会出现软键盘.编辑文本确实需要重点,但键盘永远不会出现.

如果我不使用适配器,只使用带有编辑文本的视图,它可以很好地工作,但只要我添加适配器,我就会遇到问题.

我的代码如下.任何帮助将非常感激.

提前致谢.

public class ParameterDialog extends DialogFragment {
Context context;

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    ArrayList<Parameter> params = this.getArguments().getParcelableArrayList(MainActivity.KEY_PARAMS_TO_DIALOG);
    String name = this.getArguments().getString(MainActivity.KEY_NAME_TO_DIALOG);

    context = getActivity();
    // Use the Builder class for convenient dialog construction
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

    ParameterDialogAdapter pda = new ParameterDialogAdapter(context,R.layout.parameter_config_string , params);

    // Inflate and set the layout for the dialog
    // Pass null as the parent view because its going in the dialog layout
    builder
            .setTitle(name)
            .setAdapter(pda,null)
            .setPositiveButton("Send", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                }
            })
            .setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                }
            });

    // Create the AlertDialog object and return it
    return builder.create();
}}
Run Code Online (Sandbox Code Playgroud)

-

public class ParameterDialogAdapter extends ArrayAdapter<Parameter> {

Context context;
int layoutResourceId;
ArrayList<Parameter> data= null;

public ParameterDialogAdapter(Context context, int layoutResourceId, ArrayList<Parameter> data) {
    super(context, layoutResourceId, data);
    this.layoutResourceId = layoutResourceId;
    this.context = context;
    this.data = data;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View row = convertView;
    //ParameterHolder holder = null;
    final LayoutInflater inflater = ((Activity)context).getLayoutInflater();
    final int p =position;
    row = inflater.inflate(layoutResourceId, parent, false);
    return row;
}}
Run Code Online (Sandbox Code Playgroud)

-布局

<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/paramValueHolder"
android:paddingRight="10dp"
android:paddingLeft="10dp"
android:paddingBottom="5dp">

<EditText
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/paramValue"
    android:hint="Value"
    android:textColor="@color/text_grey" />
Run Code Online (Sandbox Code Playgroud)

- 显示对话框

ParameterDialog pDialog = new ParameterDialog ();

        Bundle bundle = new Bundle();
        bundle.putString(KEY_NAME_TO_DIALOG,action.getName());
        bundle.putParcelableArrayList(KEY_PARAMS_TO_DIALOG, params);
        pDialog.setArguments(bundle);

        pDialog.setArguments(bundle);
        pDialog.show(ft, "parameter_dialog");
Run Code Online (Sandbox Code Playgroud)

Gar*_*wan 6

我在使用FragmentDialog包含a 时遇到了类似的问题ListView.ListView包含的EditText小部件中的项目未显示软键盘.

我发现的一种解决方法是EditText在其根布局中放置一个不可见的FragmentDialog.

例如:

list_layout.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:id="@android:id/list"/>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:visibility="gone" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

DialogFragment创建视图如下:

@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
    LayoutInflater inflater = getActivity().getLayoutInflater();
    View listLayout = inflater.inflate(R.layout.list_layout, null);
    mListView = (ListView) listLayout.findViewById(android.R.id.list);
    return new AlertDialog.Builder(getActivity()).setView(listLayout).create();
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState)
{
    super.onActivityCreated(savedInstanceState);
    //set adapter
    mListView.setAdapter(mListAdapter);
}
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助

  • 魔法可能会起作用一段时间,但有一天它就不起作用了,你只是推迟了问题的解决。 (2认同)