Как создать экземпляр макета для пользовательских настроек, используя атрибут android: layout

Я могу установить соответствующий макет для предпочтений с помощью атрибута android: layout . Для примера

<Preference
  android:key="friction" 
  android:title="@string/friction" 
  android:layout="@layout/friction_fragment" 
  android:shouldDisableView="true" 
  android:defaultValue="30" 
  android:enabled="true"
  android:selectable="true" 
  android:summary="Bite friction">
</Preference>

, где макет равен

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
    <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:text="@string/friction" android:textAppearance="?android:attr/textAppearanceLarge" android:layout_height="wrap_content" android:layout_gravity="center_horizontal"></TextView>
    <SeekBar android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/sbFriction"></SeekBar>
    <TextView android:text="@string/friction_little" android:id="@+id/txtSummary" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
    <Button android:text="Button" android:id="@+id/btnFriction" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>

</LinearLayout>

, я могу получить представления в OnCreate в PreferenceActivity

    Preference fric = (Preference)this.findPreference("friction");
    View v = fric.getView(null, null);
    SeekBar sbFriction = (SeekBar)v.findViewById(R.id.sbFriction);
    sbFriction.setOnSeekBarChangeListener(this);
    Button btnFric = (Button) v.findViewById(R.id.btnFriction);
    btnFric.setOnClickListener(m_onClick);

, но эти прослушиватели событий, которые я установил, не запускаются. Как я могу поймать эти события для пример - нажмите с кнопки. Изменить. Нет, исключение не сработало. Вот более подробный код

public class SettingsActivity extends PreferenceActivity implements OnPreferenceChangeListener, OnSeekBarChangeListener
{

    private TextView m_txtSummary;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);

        ListPreference difficulty = (ListPreference)this.findPreference("difficulty");
        difficulty.setSummary(difficulty.getEntry());
        difficulty.setOnPreferenceChangeListener(this);

        Preference fric = (Preference)this.findPreference("friction");
        View v = fric.getView(null, null);
        SeekBar sbFriction = (SeekBar)v.findViewById(R.id.sbFriction);
        sbFriction.setOnSeekBarChangeListener(this);
        Button btnFric = (Button) v.findViewById(R.id.btnFriction);
        btnFric.setOnClickListener(m_onClick);

        m_txtSummary = (TextView)v.findViewById(R.id.txtSummary);

        fric.setSummary(fric.toString());
        fric.setOnPreferenceChangeListener(this);

        CheckBoxPreference music = (CheckBoxPreference)this.findPreference("music");
        music.setOnPreferenceChangeListener(this);
    }

    private OnClickListener m_onClick = new OnClickListener(){

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            v.getId();
        }

    };

    @Override
    public boolean onPreferenceChange(Preference preference, Object newValue) {
        if(newValue instanceof Boolean)
            return true;

        preference.setSummary(newValue.toString());
        return true;
    }

    @Override
    public void onProgressChanged(SeekBar v, int nProgress, boolean arg2) {
        // TODO Auto-generated method stub
        m_txtSummary.append(" " + nProgress);
        m_txtSummary.invalidate();
    }

    @Override
    public void onStartTrackingTouch(SeekBar arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onStopTrackingTouch(SeekBar arg0) {
        // TODO Auto-generated method stub
        //notifyChanged();
    }
}
9
задан CopsOnRoad 19 June 2018 в 21:07
поделиться