我正在尝试遵循有关设置的 Android API 指南,更具体地说,我正在尝试通过遵循这部分文本来创建此数字选择器对话框。但我无法让它工作。当我按下应该启动自定义对话框的首选项时,没有任何反应。我想我必须在某个地方打电话,但我不知道怎么做。
这是我的 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。设置活动.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);
}
}