【问题标题】:Soft Keyboard Doesnt show when using adapter in DialogFragment在 DialogFragment 中使用适配器时软键盘不显示
【发布时间】:2014-01-21 06:37:51
【问题描述】:

我有一个自定义的 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();
}}

-

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;
}}

-布局

<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" />

-显示对话框的代码

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

【问题讨论】:

  • 是否需要使用适配器? - 见stackoverflow.com/questions/12876624/…
  • 您好,很遗憾,是的。上面是一个简化的示例,演示了我遇到的问题。我的实际代码更复杂,适配器似乎是最好的方法。如果一切都失败了,我想我将不得不删除它。

标签: android android-arrayadapter android-dialogfragment


【解决方案1】:

我在使用包含ListViewFragmentDialog 时遇到了类似的问题。 ListView 中的项目包含未调出软键盘的 EditText 小部件。

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

例如:

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>

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

希望对你有帮助

【讨论】:

  • 这背后的逻辑是什么?
  • 它的工作,但它背后的逻辑是什么,我仍然无法理解。
  • 魔术可能会工作一段时间,但有一天它不会,你只是推迟了这个问题。
【解决方案2】:

将您的 list_item_layout 文件的 block descendants 属性设置为 true.. 希望对你有帮助

【讨论】:

  • 谢谢,我添加了 android:descendantFocusability="blocksDescendants" 到根线性布局,但仍然没有调出键盘并阻止我专注于编辑文本。
猜你喜欢
  • 1970-01-01
  • 2023-03-08
  • 2022-11-18
  • 1970-01-01
  • 2016-12-21
  • 2013-12-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多