Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在android中创建一个简单的设置页面?_Java_Android_Android Studio_Settings - Fatal编程技术网

Java 如何在android中创建一个简单的设置页面?

Java 如何在android中创建一个简单的设置页面?,java,android,android-studio,settings,Java,Android,Android Studio,Settings,我是“Android应用开发”领域的新手,有一个问题: 如何轻松为我的应用创建一个外观整洁的设置页面 在标签页上有一些标题和一些大按钮,你可以进入一个新的页面 我正在使用Android Studio,知道如何创建新页面、类等。使用 开发人员站点的示例代码: public class PreferenceWithHeaders extends PreferenceActivity { @Override protected void onCreate(Bundle savedIns

我是“Android应用开发”领域的新手,有一个问题:

如何轻松为我的应用创建一个外观整洁的设置页面

在标签页上有一些标题和一些大按钮,你可以进入一个新的页面

我正在使用Android Studio,知道如何创建新页面、类等。

使用

开发人员站点的示例代码:

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

        // Add a button to the header list.
        if (hasHeaders()) {
            Button button = new Button(this);
            button.setText("Some action");
            setListFooter(button);
        }
    }

    /**
     * Populate the activity with the top-level headers.
     */
    @Override
    public void onBuildHeaders(List<Header> target) {
        loadHeadersFromResource(R.xml.preference_headers, target);
    }

    /**
     * This fragment shows the preferences for the first header.
     */
    public static class Prefs1Fragment extends PreferenceFragment {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            // Make sure default values are applied.  In a real app, you would
            // want this in a shared function that is used to retrieve the
            // SharedPreferences wherever they are needed.
            PreferenceManager.setDefaultValues(getActivity(),
                    R.xml.advanced_preferences, false);

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

    /**
     * This fragment contains a second-level set of preference that you
     * can get to by tapping an item in the first preferences fragment.
     */
    public static class Prefs1FragmentInner extends PreferenceFragment {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            // Can retrieve arguments from preference XML.
            Log.i("args", "Arguments: " + getArguments());

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

    /**
     * This fragment shows the preferences for the second header.
     */
    public static class Prefs2Fragment extends PreferenceFragment {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            // Can retrieve arguments from headers XML.
            Log.i("args", "Arguments: " + getArguments());

            // Load the preferences from an XML resource
            addPreferencesFromResource(R.xml.preference_dependencies);
        }
    }
}
带有标题的公共类首选项扩展了首选项活动{
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//在标题列表中添加一个按钮。
if(hashreaders()){
按钮按钮=新按钮(此按钮);
setText(“某些操作”);
setListFooter(按钮);
}
}
/**
*使用顶级标题填充活动。
*/
@凌驾
public void onBuildHeaders(列表目标){
loadHeadersFromResource(R.xml.preference_头,目标);
}
/**
*此片段显示第一个标头的首选项。
*/
公共静态类Prefs1Fragment扩展了PreferenceFragment{
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//确保应用了默认值。在真正的应用程序中,您将
//希望在用于检索
//在任何需要的地方共享数据引用。
PreferenceManager.setDefaultValues(getActivity(),
R.xml.advanced_preferences,false);
//从XML资源加载首选项
addPreferencesFromResource(R.xml.fragmented_preferences);
}
}
/**
*此片段包含您需要的第二级首选项集
*可以通过点击第一个首选项片段中的一个项目来访问。
*/
公共静态类Prefs1FragmentInner扩展了PreferenceFragment{
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//可以从首选项XML检索参数。
Log.i(“args”,“Arguments:+getArguments());
//从XML资源加载首选项
addPreferencesFromResource(R.xml.fragmented\u preferences\u inner);
}
}
/**
*此片段显示第二个标头的首选项。
*/
公共静态类Prefs2Fragment扩展了PreferenceFragment{
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//可以从XML头中检索参数。
Log.i(“args”,“Arguments:+getArguments());
//从XML资源加载首选项
addPreferencesFromResource(R.xml.preference\u依赖项);
}
}
}
preference_headers资源描述了要显示的标题以及与之关联的片段。它是:

<header android:fragment="com.example.android.apis.preference.PreferenceWithHeaders$Prefs1Fragment"
        android:icon="@drawable/ic_settings_applications"
        android:title="Prefs 1"
        android:summary="An example of some preferences." />

<header android:fragment="com.example.android.apis.preference.PreferenceWithHeaders$Prefs2Fragment"
        android:icon="@drawable/ic_settings_display"
        android:title="Prefs 2"
        android:summary="Some other preferences you can see.">
    <!-- Arbitrary key/value pairs can be included with a header as
         arguments to its fragment. -->
    <extra android:name="someKey" android:value="someHeaderValue" />
</header>

<header android:icon="@drawable/ic_settings_display"
        android:title="Intent"
        android:summary="Launches an Intent.">
    <intent android:action="android.intent.action.VIEW"
            android:data="http://www.android.com" />
</header>


第一个标头由Prefs1Fragment显示,它从以下XML资源填充自身:

<PreferenceCategory
        android:title="@string/inline_preferences">

    <CheckBoxPreference
            android:key="checkbox_preference"
            android:title="@string/title_checkbox_preference"
            android:summary="@string/summary_checkbox_preference" />

</PreferenceCategory>

<PreferenceCategory
        android:title="@string/dialog_based_preferences">

    <EditTextPreference
            android:key="edittext_preference"
            android:title="@string/title_edittext_preference"
            android:summary="@string/summary_edittext_preference"
            android:dialogTitle="@string/dialog_title_edittext_preference" />

    <ListPreference
            android:key="list_preference"
            android:title="@string/title_list_preference"
            android:summary="@string/summary_list_preference"
            android:entries="@array/entries_list_preference"
            android:entryValues="@array/entryvalues_list_preference"
            android:dialogTitle="@string/dialog_title_list_preference" />

</PreferenceCategory>

<PreferenceCategory
        android:title="@string/launch_preferences">

    <!-- This PreferenceScreen tag sends the user to a new fragment of
         preferences.  If running in a large screen, they can be embedded
         inside of the overall preferences UI. -->
    <PreferenceScreen
            android:fragment="com.example.android.apis.preference.PreferenceWithHeaders$Prefs1FragmentInner"
            android:title="@string/title_fragment_preference"
            android:summary="@string/summary_fragment_preference">
        <!-- Arbitrary key/value pairs can be included for fragment arguments -->
        <extra android:name="someKey" android:value="somePrefValue" />
    </PreferenceScreen>

    <!-- This PreferenceScreen tag sends the user to a completely different
         activity, switching out of the current preferences UI. -->
    <PreferenceScreen
            android:title="@string/title_intent_preference"
            android:summary="@string/summary_intent_preference">

        <intent android:action="android.intent.action.VIEW"
                android:data="http://www.android.com" />

    </PreferenceScreen>

</PreferenceCategory>

<PreferenceCategory
        android:title="@string/preference_attributes">

    <CheckBoxPreference
            android:key="parent_checkbox_preference"
            android:title="@string/title_parent_preference"
            android:summary="@string/summary_parent_preference" />

    <!-- The visual style of a child is defined by this styled theme attribute. -->
    <CheckBoxPreference
            android:key="child_checkbox_preference"
            android:dependency="parent_checkbox_preference"
            android:layout="?android:attr/preferenceLayoutChild"
            android:title="@string/title_child_preference"
            android:summary="@string/summary_child_preference" />

</PreferenceCategory>


注意,这个XML资源包含一个包含另一个片段的首选项屏幕,Prefs1FragmentInner在这里实现。这允许用户遍历偏好的层次结构;按back将弹出堆栈中的每个片段,以返回到以前的首选项


有关实现片段本身的信息,请参阅PreferenceFragment。

从2019年起,建议使用

PreferenceActivity
实际上在API级别29()中被弃用:

该类在API级别29中被弃用。 使用AndroidX首选项库在所有设备上实现一致的行为。有关使用AndroidX首选项库的更多信息,请参阅设置


您好,我编写了一个库,用于轻松设置首选项屏幕。请看:plz,帮我一个忙(因为我是新手),写下这些代码应该写在哪里(在哪些文件中)。还有,按钮应该如何调用/打开该页面。其他示例:我喜欢我被否决的方式,即使我引用了官方文档。这是真的。我仍然看到有人引用不推荐的api。