在DialogFragment使用适配器时,软键盘显示犯规 [英] Soft Keyboard Doesnt show when using adapter in DialogFragment

查看:925
本文介绍了在DialogFragment使用适配器时,软键盘显示犯规的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有在其中具有ArrayAdapter具有一些editTexts在它的自定义DialogFragment。当显示对话框软键盘不会出现,即使我美元的编辑文本p $ PSS。编辑文本确实需要焦点,但键盘永远不会到来了。

如果我不使用转接器,只需使用一个视图与它,只要我添加适配器我得到的问题,完美的作品,但编辑的文本。

我的code如下。任何帮助将是非常美联社preciated。

先谢谢了。

 公共类ParameterDialog扩展DialogFragment {
上下文语境;@覆盖
公共对话框onCreateDialog(捆绑savedInstanceState){    ArrayList的<参数> PARAMS = this.getArguments()getParcelableArrayList(MainActivity.KEY_PARAMS_TO_DIALOG)。
    。字符串名称= this.getArguments()的getString(MainActivity.KEY_NAME_TO_DIALOG);    上下文= getActivity();
    //使用生成器类简单的对话框建设
    AlertDialog.Builder建设者=新AlertDialog.Builder(getActivity());    ParameterDialogAdapter PDA =新ParameterDialogAdapter(背景下,R.layout.parameter_config_string,则params);    //充气并为对话设置布局
    作为父视图//传递空值,因为它在该对话框的布局会
    建设者
            .setTitle(名)
            .setAdapter(PDA,NULL)
            .setPositiveButton(发送,新DialogInterface.OnClickListener(){
                公共无效的onClick(DialogInterface对话,诠释的id){
                }
            })
            .setNegativeButton(android.R.string.cancel,新DialogInterface.OnClickListener(){
                公共无效的onClick(DialogInterface对话,诠释的id){
                }
            });    //创建AlertDialog对象,并将其返回
    返回builder.create();
}}

-

 公共类ParameterDialogAdapter扩展ArrayAdapter<参数> {上下文语境;
INT layoutResourceId;
ArrayList的<参数>数据= NULL;公共ParameterDialogAdapter(上下文的背景下,诠释layoutResourceId,ArrayList的<参数>数据){
    超级(上下文,layoutResourceId,数据);
    this.layoutResourceId = layoutResourceId;
    this.context =背景;
    this.data =数据;
}@覆盖
公共查看getView(INT位置,查看convertView,父母的ViewGroup){    查看排= convertView;
    // ParameterHolder持有人= NULL;
    最后LayoutInflater吹气=((活动)上下文).getLayoutInflater();
    最终诠释P =位置;
    行= inflater.inflate(layoutResourceId,父母,假);
    返回行;
}}

-layout

 <的LinearLayout
机器人:方向=横向
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:ID =@ + ID / paramValueHolder
机器人:paddingRight =10dp
机器人:paddingLeft =10dp
机器人:paddingBottom会=5DP><的EditText
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
    机器人:ID =@ + ID / paramValue
    机器人:提示=值
    机器人:文字颜色=@色/ text_grey/>

- code键来显示对话框

  ParameterDialog pDialog =新ParameterDialog();        束束=新包();
        bundle.putString(KEY_NAME_TO_DIALOG,action.getName());
        bundle.putParcelableArrayList(KEY_PARAMS_TO_DIALOG,则params);
        pDialog.setArguments(包);        pDialog.setArguments(包);
        pDialog.show(英尺parameter_dialog);


解决方案

我也碰到过类似的问题,而使用含的ListView FragmentDialog 。在的ListView的项目包含的EditText 未调出软键盘的小部件。

我发现了一个解决方法是将一个无形的的EditText 的根布局中的 FragmentDialog

例如:

list_layout.xml:

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:方向=垂直
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent>    < ListView控件
        机器人:layout_width =match_parent
        机器人:layout_height =0dp
        机器人:layout_weight =1
        机器人:ID =@机器人:ID /目录/>    <的EditText
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT
        机器人:知名度=水涨船高/>
< / LinearLayout中>

DialogFragment创建意见如下:

  @覆盖
公共对话框onCreateDialog(捆绑savedInstanceState)
{
    LayoutInflater吹气= getActivity()getLayoutInflater()。
    查看listLayout = inflater.inflate(R.layout.list_layout,NULL);
    mListView =(ListView控件)listLayout.findViewById(android.R.id.list);
    返回新AlertDialog.Builder(getActivity())的setView(listLayout).create();
}@覆盖
公共无效onActivityCreated(@Nullable捆绑savedInstanceState)
{
    super.onActivityCreated(savedInstanceState);
    //设置适配器
    mListView.setAdapter(mListAdapter);
}

希望这有助于

I have a custom DialogFragment which has an ArrayAdapter in it which has some editTexts in it. When the dialog is shown the soft keyboard does not appear even if i press on the edit text. The edit text does take focus but the keyboard never comes up.

If i do not use an adapter and just use a view with an edit text it works perfectly but as soon as i add the adapter i get the issue.

My code is below. Any help would be really appreciated.

Thanks in Advance.

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

-Layout

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

-Code to Show Dialog

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

解决方案

I have come across a similar problem while using a FragmentDialog containing a ListView. The items in the ListView contained EditText widgets that did not bring up the soft keyboard.

A workaround I discovered was to place an invisible EditText within the root layout of the FragmentDialog.

For example:

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 creates the views as follows:

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

Hope this helps

这篇关于在DialogFragment使用适配器时,软键盘显示犯规的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆