如何在 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