返回

Android Studio插件 (三):构建设置页面

Android

在本文中,我们将看到如何使用持久化的数据来创建设置页面。您可以在GitHub上找到本系列的所有代码,还可以在对应的分支上查看最新更新。

首先,我们需要创建一个新的活动来容纳我们的设置页面。为此,请右键单击包资源管理器中的项目文件夹,然后选择“新建”>“活动”>“空白活动”。将活动命名为“SettingsActivity”并单击“完成”。

接下来,我们需要在SettingsActivity中添加一个布局。为此,请右键单击该活动,然后选择“设计”>“编辑布局”。在布局编辑器中,将以下XML代码添加到其中:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".SettingsActivity">

    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Settings"
        android:textSize="24sp"
        android:gravity="center" />

    <Switch
        android:id="@+id/switch1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Option 1" />

    <Switch
        android:id="@+id/switch2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Option 2" />

    <Button
        android:id="@+id/save_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Save" />

</LinearLayout>

在上面的XML代码中,我们首先添加了一个垂直线性布局作为根布局。然后,我们添加了一个TextView作为标题,两个Switch作为设置选项,以及一个Button来保存用户所做的更改。

接下来,我们需要在SettingsActivity中添加一些Java代码来处理用户交互。为此,请右键单击该活动,然后选择“打开Java文件”。在SettingsActivity.java文件中,添加以下Java代码:

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Switch;

public class SettingsActivity extends AppCompatActivity {

    private Switch switch1;
    private Switch switch2;
    private Button saveButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);

        switch1 = findViewById(R.id.switch1);
        switch2 = findViewById(R.id.switch2);
        saveButton = findViewById(R.id.save_button);

        saveButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Save the user's settings to persistent storage
                SharedPreferences sharedPreferences = getSharedPreferences("Settings", MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putBoolean("switch1", switch1.isChecked());
                editor.putBoolean("switch2", switch2.isChecked());
                editor.apply();

                // Close the settings activity
                finish();
            }
        });
    }
}

在上面的Java代码中,我们首先声明了三个变量来存储Switch和Button控件。然后,我们在onCreate()方法中初始化这些变量并设置Button控件的点击侦听器。在点击侦听器中,我们使用SharedPreferences来保存用户所做的更改到持久化存储中。最后,我们调用finish()方法来关闭SettingsActivity。

现在,我们可以运行我们的应用程序并测试设置页面。当我们点击“保存”按钮时,用户所做的更改将被保存到持久化存储中。下次用户打开我们的应用程序时,这些更改将被加载并应用。