Android 带有包含时间选择器的自定义本机(不兼容)dialogpreference的Appcompatactivity
我正在AndroidAndroid 带有包含时间选择器的自定义本机(不兼容)dialogpreference的Appcompatactivity,android,settings,android-timepicker,preferencescreen,dialog-preference,Android,Settings,Android Timepicker,Preferencescreen,Dialog Preference,我正在AndroidAppCompatActivity中构建一个首选项/设置屏幕。一个要求是带有时间选择器的自定义[DialogPreference][1] DialogPreference必须为“本机”,这意味着不是所述和所述的兼容版本 AppCompative活动的代码: ... public class SettingsActivity extends AppCompatActivity { @Override protected void onCreate(Bundle
AppCompatActivity
中构建一个首选项/设置屏幕。一个要求是带有时间选择器的自定义[DialogPreference][1]
DialogPreference必须为“本机”,这意味着不是所述和所述的兼容版本
AppCompative活动的代码:
...
public class SettingsActivity extends AppCompatActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_preferences);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_settings);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
}
...
public class SettingsActivity extends AppCompatActivity
{
public static final String KEY_PREF_NOTIFY = "pref_notify";
public static final String KEY_PREF_NOTIFY_TIME = "pref_notify_time";
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_preferences);
}
}
activity_preferences.xml的布局:
...
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<fragment
android:name="nl.waywayway.broodkruimels.SettingsFragment"
android:id="@+id/settings_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.widget.NestedScrollView>
...
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<fragment
android:name="nl.waywayway.broodkruimels.SettingsFragment"
android:id="@+id/settings_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.widget.NestedScrollView>
preferences.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<SwitchPreference
android:key="pref_notify"
android:title="@string/pref_notify"
android:summary="@string/pref_notify_summ"
android:defaultValue="false" />
<nl.waywayway.broodkruimels.TimePreference
android:dependency="pref_notify"
android:key="pref_notify_time"
android:title="@string/pref_notify_time"
android:summary="@string/pref_notify_time_summ"
android:defaultValue="390" />
</PreferenceScreen>
...
<android.support.v7.preference.PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.preference.SwitchPreferenceCompat
android:key="pref_notify"
android:title="@string/pref_notify"
android:summary="@string/pref_notify_summ"
android:defaultValue="false" />
<android.support.v7.preference.Preference
android:dependency="pref_notify"
android:key="pref_notify_time"
android:title="@string/pref_notify_time"
android:summary="@string/pref_notify_time_summ"
android:defaultValue="390" />
</android.support.v7.preference.PreferenceScreen>
preferences\u timepicker\u dialog.xml文件如下所示:
...
<TimePicker
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/preferences_timepicker"
android:layout_width="match_parent"
android:layout_height="match_parent" />
。。。
结果如下图所示。在装有安卓7的Moto G5 plus手机上
问题:
应该有两种偏好。但是,自定义对话框首选项未显示在“设置”列表中。
这里出了什么问题?
AppCompatActivity是否实际与“本机”对话框首选项一起工作
TimePreference类实际上是从preferences xml实例化的,可以从构造函数中记录。也没有编译时错误,也没有运行时错误。最后我找到了一种不同的方法,它看起来干净,经过测试,可以在Android 4到7的真实设备上运行。首选项将显示在首选项屏幕中 此外,时间选择器对话框以横向方向正确显示。这在某些设备上是一个问题。看
- 在首选项xml文件中包括常规
项首选项
- 使用onPreferenceTreeClick()在此首选项上设置单击侦听器
- 单击此首选项时,显示常规(非兼容性库) 如《拾荒者指南》中所述 ()
- 在SharedReferences中手动保存时间选择器上设置的时间
...
public class SettingsActivity extends AppCompatActivity
{
public static final String KEY_PREF_NOTIFY = "pref_notify";
public static final String KEY_PREF_NOTIFY_TIME = "pref_notify_time";
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_preferences);
}
}
布局文件activity_preferences.xml:
...
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<fragment
android:name="nl.waywayway.broodkruimels.SettingsFragment"
android:id="@+id/settings_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.widget.NestedScrollView>
...
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<fragment
android:name="nl.waywayway.broodkruimels.SettingsFragment"
android:id="@+id/settings_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.widget.NestedScrollView>
preferences.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<SwitchPreference
android:key="pref_notify"
android:title="@string/pref_notify"
android:summary="@string/pref_notify_summ"
android:defaultValue="false" />
<nl.waywayway.broodkruimels.TimePreference
android:dependency="pref_notify"
android:key="pref_notify_time"
android:title="@string/pref_notify_time"
android:summary="@string/pref_notify_time_summ"
android:defaultValue="390" />
</PreferenceScreen>
...
<android.support.v7.preference.PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.preference.SwitchPreferenceCompat
android:key="pref_notify"
android:title="@string/pref_notify"
android:summary="@string/pref_notify_summ"
android:defaultValue="false" />
<android.support.v7.preference.Preference
android:dependency="pref_notify"
android:key="pref_notify_time"
android:title="@string/pref_notify_time"
android:summary="@string/pref_notify_time_summ"
android:defaultValue="390" />
</android.support.v7.preference.PreferenceScreen>
最后,我找到了一种不同的方法,它看起来干净,经过测试,可以在Android 4到7的真实设备上运行。首选项将显示在首选项屏幕中 此外,时间选择器对话框以横向方向正确显示。这在某些设备上是一个问题。看
- 在首选项xml文件中包括常规
项首选项
- 使用onPreferenceTreeClick()在此首选项上设置单击侦听器
- 单击此首选项时,显示常规(非兼容性库) 如《拾荒者指南》中所述 ()
- 在SharedReferences中手动保存时间选择器上设置的时间
...
public class SettingsActivity extends AppCompatActivity
{
public static final String KEY_PREF_NOTIFY = "pref_notify";
public static final String KEY_PREF_NOTIFY_TIME = "pref_notify_time";
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_preferences);
}
}
布局文件activity_preferences.xml:
...
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<fragment
android:name="nl.waywayway.broodkruimels.SettingsFragment"
android:id="@+id/settings_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.widget.NestedScrollView>
...
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<fragment
android:name="nl.waywayway.broodkruimels.SettingsFragment"
android:id="@+id/settings_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.widget.NestedScrollView>
preferences.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<SwitchPreference
android:key="pref_notify"
android:title="@string/pref_notify"
android:summary="@string/pref_notify_summ"
android:defaultValue="false" />
<nl.waywayway.broodkruimels.TimePreference
android:dependency="pref_notify"
android:key="pref_notify_time"
android:title="@string/pref_notify_time"
android:summary="@string/pref_notify_time_summ"
android:defaultValue="390" />
</PreferenceScreen>
...
<android.support.v7.preference.PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.preference.SwitchPreferenceCompat
android:key="pref_notify"
android:title="@string/pref_notify"
android:summary="@string/pref_notify_summ"
android:defaultValue="false" />
<android.support.v7.preference.Preference
android:dependency="pref_notify"
android:key="pref_notify_time"
android:title="@string/pref_notify_time"
android:summary="@string/pref_notify_time_summ"
android:defaultValue="390" />
</android.support.v7.preference.PreferenceScreen>