EditText внутри пользовательского ListPreference: без клавиатуры

Используя пользовательскую ListPreference, я пытаюсь попросить пользователя выбрать между 2 предопределенными значениями и пользовательским значением, которое он может установить, используя текст редактирования. Кажется, все работает, за исключением того, что клавиатура не появляется, когда я нажимаю на 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
                         */
                        }
                    }
                });
            }
        }
    }
}

Обратите внимание, что это основано на другом ответе, найденном здесь: пользовательская строка в listPreference?

Изменить: я добавил несколько кодов выше (см. onPrepareDialogBuilder), потому что он может прийти из построенного диалога.

1 ответ

Решение

Хорошо, мне удалось решить это.

Как сказано здесь: /questions/39852988/android-edittext-v-dialoge-ne-podnimaet-programmnuyu-klaviaturu/39853004#39853004

Похоже, проблема (по крайней мере, в моем случае) заключается в том, что поскольку место, где вы вводите текст, изначально скрыто (или вложено, или что-то еще), 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 конструктор, так что диалог уже построен, когда я это делаю.

Другие вопросы по тегам