如何在 Android 中向 PreferenceScreen 添加一个按钮?
此示例演示如何在 Android 中向 PreferenceScreen 添加按钮。
步骤 1 − 在 Android Studio 中创建新项目,转到 File rArr; New Project 并填写所有必要详细信息以创建新项目。
步骤 2 − 将以下代码添加到 res/layout/activity_main.xml。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <FrameLayout android:id="@+id/settings" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>
步骤 3 − 将以下代码添加到 src/MainActivity.java
import android.os.Bundle;
import android.widget.Toast;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.preference.Preference;
import androidx.preference.PreferenceFragmentCompat;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
getSupportFragmentManager().beginTransaction().replace(R.id.settings, new SettingsFragment()).commit();
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
}
}
public static class SettingsFragment extends PreferenceFragmentCompat {
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.root_preferences, rootKey);
Preference button = getPreferenceManager().findPreference("toastMsg");
if (button != null) {
button.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference arg0) {
Toast.makeText(getActivity(), "Preference button is clicked",
Toast.LENGTH_SHORT).show();
return true;
}
});
}
}
}
}步骤 4 − 在 root_preferences.xml 中添加以下代码
<PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto"> <PreferenceCategory app:title="@string/messages_header"> <EditTextPreference app:key="signature" app:title="@string/signature_title" app:useSimpleSummaryProvider="true" /> <ListPreference app:defaultValue="reply" app:entries="@array/reply_entries" app:entryValues="@array/reply_values" app:key="reply" app:title="@string/reply_title" app:useSimpleSummaryProvider="true" /> <Preference app:key="toastMsg" app:title="Show me a Toast" /> </PreferenceCategory> <PreferenceCategory app:title="@string/sync_header"> <SwitchPreferenceCompat app:key="sync" app:title="@string/sync_title" /> <SwitchPreferenceCompat app:dependency="sync" app:key="attachment" app:summaryOff="@string/attachment_summary_off" app:summaryOn="@string/attachment_summary_on" app:title="@string/attachment_title" /> </PreferenceCategory> </PreferenceScreen>
步骤 5 − 将以下代码添加到 androidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="app.com.sample"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity" android:label="@string/title_activity_settings"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
现在尝试运行该应用程序。我认为你已将 Android 移动设备与计算机连接。要从 Android Studio 运行应用,请打开项目的一个活动文件并单击工具栏上的运行
图标。选择移动设备作为选项,然后查看移动设备,它将显示默认屏幕 −


单击 此处 下载项目代码
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP