从SettingsFragment打开自定义对话框

时间:2012-08-08 11:46:52

标签: android

我正在尝试按照设置上的Android API指南,更具体地说,我正在尝试按照this number picker dialog创建this part of the text。但我无法让它发挥作用。当我按下应该启动自定义对话框的首选项时,没有任何反应。我想我必须在某个地方打电话,但我无法弄明白。

这是我的MainActivity.java:

public class MainActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_activity_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.menu_settings_fragment:
        // Display the fragment as the main content
        Intent i = new Intent();
        i.setClass(this, SettingsActivity.class);
        startActivityForResult(i, 0);
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
} 
}

它很好地启动了SettingsActivity.java。 SettingsActivity.java:

public class SettingsActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Display the fragment as the main content.
    getFragmentManager().beginTransaction()
            .replace(android.R.id.content, new SettingsFragment()).commit();
}
}

转到SettingsFragment.java:

public class SettingsFragment extends PreferenceFragment {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Load the preferences from an XML resource
    addPreferencesFromResource(R.xml.preferences);
}
}

启动preferences.xml:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >

    <PreferenceCategory
        android:key="pref_key_category_settings"
        android:title="@string/pref_category1_title" >
        <CheckBoxPreference
            android:defaultValue="true"
            android:key="pref_checkbox"
            android:summary="@string/pref_checkbox_summ"
            android:title="@string/pref_sync" />
        <CheckBoxPreference
            android:defaultValue="true"
            android:key="pref_checkbox_choose_int"
            android:summary="@string/pref_checkbox_choose_string_summ"
            android:title="@string/pref_choose_string" />

        <Preference
            android:dependency="pref_checkbox_choose_int"
            android:key="pref_key_num_scroller"
            android:summary="@string/pref_num_scroller_summ"
            android:title="@string/pref_num_scroller"/>
    </PreferenceCategory>

</PreferenceScreen>

正如我所说:当我点击pref_key_num_scroller时没有任何反应。

我不知道如何调用NumberPickerPreference.java:

public class NumberPickerPreference extends DialogPreference {

public NumberPickerPreference(Context context, AttributeSet attrs) {
    super(context, attrs);

    setDialogLayoutResource(R.layout.numberpicker_dialog);
    setPositiveButtonText(android.R.string.ok);
    setNegativeButtonText(android.R.string.cancel);
}
}

1 个答案:

答案 0 :(得分:2)

使用其完全限定名称将XML中的Preference更改为NumberPickerPreference:

<com.something.settings.NumberPickerPreference 
    android:dependency="pref_checkbox_choose_int"
    android:key="pref_key_num_scroller"
    android:summary="@string/pref_num_scroller_summ"
    android:title="@string/pref_num_scroller"/>