自定义ListPreference中的EditText:没有键盘

时间:2013-05-30 20:00:46

标签: android android-layout sharedpreferences android-edittext listpreference

使用自定义ListPreference,我试图要求用户在2个预定义值和他可以使用edittext设置的自定义值之间进行选择。 一切似乎都有效,除了单击EditText时键盘没有出现。 然而,EditText似乎正如通常那样获得焦点。

这是我正在获得的图像(看到焦点似乎没问题) https://www.dropbox.com/s/isejjkveonwnb3f/Screenshot_2013-05-30-21-43-41.png

以下是自定义列表首选项行的布局:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingBottom="8dip"
    android:paddingTop="8dip"
    android:paddingLeft="10dip"
    android:paddingRight="10dip">



    <EditText
        android:id="@+id/ET_prefs_customText"
    android:layout_width="0dip"
    android:layout_weight="1"
    android:hint="@string/SP_address_hint"
    android:layout_height="wrap_content"
        />
            <RadioButton
                android:id="@+id/custom_list_view_row_radio_button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:checked="false" />

</LinearLayout>

这是我的CustomListPreference代码:

public class CustomListPreference extends ListPreference
{   
    /*
     * removed useless stuff
    */
        @Override
protected void onPrepareDialogBuilder(Builder builder)
{
    entries = getEntries();
    entryValues = getEntryValues();

    if (entries == null || entryValues == null || entries.length != entryValues.length )
    {
        throw new IllegalStateException(
                "ListPreference requires an entries array and an entryValues array which are both the same length");
    }

    customListPreferenceAdapter = new CustomListPreferenceAdapter(mContext);

    builder.setAdapter(customListPreferenceAdapter, new DialogInterface.OnClickListener()
    {
        public void onClick(DialogInterface dialog, int which)
        {

        }
    });
}

    private class CustomListPreferenceAdapter extends BaseAdapter
    {        
        private SharedPreferences prefs;
        public CustomListPreferenceAdapter(Context context)
        {
            this.prefs = PreferenceManager.getDefaultSharedPreferences(context);

        }

        /*
         * removed usual adapter stuff (getCount, getItem...)
         */

        public View getView(final int position, View convertView, ViewGroup parent)
        {  
            View row = convertView;

            if(row == null)
            {
                //If it's not the last one: use a normal holder...
                if(position < 2)
                {
                    NormalHolder holder = null;

                    row = mInflater.inflate(R.layout.normal_list_preference_row, parent, false);
                    if(prefs.getString(mContext.getString(R.string.SP_address), "0").equals(entryValues[position])) {
                        holder = new NormalHolder(row, position,true);
                    } else {
                        holder = new NormalHolder(row, position,false);
                    }


                    row.setTag(holder);
                    row.setClickable(true);
                    row.setOnClickListener(new View.OnClickListener()
                    {
                        public void onClick(View v)
                        {
                            for(RadioButton rb : rButtonList)
                            {
                                if(rb.getId() != position)
                                    rb.setChecked(false);
                            }

                            int index = position;
                            String value = entryValues[index].toString();
                            Log.v("Editor", "putting string" + value);
                            editor.putString(mContext.getString(R.string.SP_address), value);
                            editor.commit();
                            Dialog mDialog = getDialog();
                            mDialog.dismiss();
                        }
                    });
                    //Otherwise, if it is the last one...
                } else {
                    //Use the custom row
                    row = mInflater.inflate(R.layout.custom_list_preference_row, parent, false);
                    String fromPref = prefs.getString(mContext.getString(R.string.SP_address), "0");
                    boolean flag=false;
                    for(CharSequence entry  : entryValues) {
                        if(entry.toString().equals(fromPref)) {
                            flag=true;
                        }
                    }
                    //And use a "custom holder"
                    CustomHolder holder;
                    if(!flag) {
                        holder = new CustomHolder(row, position, fromPref, true);
                    } else {
                        holder = new CustomHolder(row, position, "", false);

                    }
                    row.setTag(holder);
                }
            }

            return row;
        }
        /*
         * This class just shows the information in row from the position and the PreferenceList entries
         */
        class NormalHolder
        {
             private TextView text = null;
             private RadioButton rButton = null;

             NormalHolder(View row, int position, boolean isCheked)
             {    
                 text = (TextView)row.findViewById(R.id.custom_list_view_row_text_view);
                 text.setText(entries[position]);
                 rButton = (RadioButton)row.findViewById(R.id.custom_list_view_row_radio_button);
                 rButton.setId(position);
                 rButton.setChecked(isCheked);

                 // also need to do something to check your preference and set the right button as checked

                 rButtonList.add(rButton);
                 rButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
                 {
                     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
                     {
                         if(isChecked)
                         {
                        /*
                         * Put stuff into SharedPreference
                         */
                         }
                     }
                 });
             }

        }
        /*
         * This class display the text within the EditText
         */
        class CustomHolder
        {
            private EditText text = null;
            private RadioButton rButton = null;

            CustomHolder(View row, int position, String pref, boolean checked)
            {    
                text = (EditText)row.findViewById(R.id.ET_prefs_customText);
                text.setText(pref);

                rButton = (RadioButton)row.findViewById(R.id.custom_list_view_row_radio_button);
                rButton.setId(position);
                rButton.setChecked(checked);

                rButtonList.add(rButton);
                rButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
                {
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
                    {
                        if(isChecked)
                        {
                        /*
                         * Put stuff into SharedPreference
                         */
                        }
                    }
                });
            }
        }
    }
}

请注意,这是基于此处找到的另一个答案:custom row in a listPreference?

修改:我在上面添加了一些代码(请参阅onPrepareDialogBuilder),因为它可能来自构建的Dialog。

1 个答案:

答案 0 :(得分:0)

好的,我设法解决了这个问题。

如上所述:https://stackoverflow.com/a/9118027/1376834

  

问题似乎是(至少在我的情况下),因为你输入文本的地方最初是隐藏的(或嵌套的东西),AlertDialog会自动设置标志WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM(或者某些组合那和WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)这样东西不会触发软输入显示。

所以我刚才补充道 我的getDialog().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM); 构造函数中的CustomHolder,以便在我这样做时已经构建了Dialog。