9

属性を介して、好みに合わせて適切なレイアウトを設定でき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>

PreferenceActivity の OnCreate でビューを取得できます

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

2 に答える 2

2

PreferenceActivity上記の方法でカスタムレイアウトを a と組み合わせて使用​​できるかどうかはわかりません。

次のいずれかを行う必要があります。

PreferenceScreenビアを使用して、アイテムに 、、 などのaddPreferencesFromResource()クラスを実装します。(CheckBoxPreferenceDialogPreferenceMultiSelectListPreferenceSharedPreferences

また

カスタム レイアウト ( を使用) でカスタムActivity( ではない) を作成し、 を使用してイベント リスナー (など)でそれらを編集し、手動で にフックします。PreferenceActivitysetContentView()SharedPreferencesPreferenceManager.getDefaultSharedPreferences()View.onClickListener()SharedPreferences.Editor

それが理にかなっていることを願っています。

于 2011-10-22T05:00:39.487 に答える