首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> 程式設計綜合問答

Android App 啟動頁雙模式選擇實作:ImageButton 與 SharedPreferences

     編輯:程式設計綜合問答

需求分析

App 啟動時顯示一個「雙選按鈕」介面:左側「A 模式」、右側「B 模式」。使用者選擇後進入對應功能流程,且下次啟動自動記住選擇(或再次詢問)。

實作步驟

1. 專案命名

Android Studio 新建專案時,「Application name」可直接填中文(如「蹲蹲樂」),Gradle 會自動處理資源字串,無需擔心編碼問題。

2. 啟動頁 Layout

<!-- res/layout/activity_start.xml -->
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:gravity="center"
    android:padding="24dp">

    <ImageButton
        android:id="@+id/btnModeA"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:src="@drawable/ic_mode_a"
        android:contentDescription="A 模式"
        android:background="?attr/selectableItemBackgroundBorderless"
        android:scaleType="fitCenter"
        android:padding="16dp" />

    <ImageButton
        android:id="@+id/btnModeB"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:src="@drawable/ic_mode_b"
        android:contentDescription="B 模式"
        android:background="?attr/selectableItemBackgroundBorderless"
        android:scaleType="fitCenter"
        android:padding="16dp" />

</LinearLayout>

使用 ImageButton 而非普通 Button,可輕鬆放置圖示並保持 Material Design 點擊回饋。

3. Activity 邏輯

public class StartActivity extends AppCompatActivity {

    private static final String PREF_NAME = "app_prefs";
    private static final String KEY_MODE = "selected_mode"; // "A" 或 "B"

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // 若已有選擇,直接跳轉主功能
        SharedPreferences prefs = getSharedPreferences(PREF_NAME, MODE_PRIVATE);
        String savedMode = prefs.getString(KEY_MODE, null);
        if (savedMode != null) {
            navigateToMain(savedMode);
            finish();
            return;
        }

        setContentView(R.layout.activity_start);

        ImageButton btnA = findViewById(R.id.btnModeA);
        ImageButton btnB = findViewById(R.id.btnModeB);

        View.OnClickListener listener = v -> {
            String mode = (v.getId() == R.id.btnModeA) ? "A" : "B";
            prefs.edit().putString(KEY_MODE, mode).apply();
            navigateToMain(mode);
            finish();
        };
        btnA.setOnClickListener(listener);
        btnB.setOnClickListener(listener);
    }

    private void navigateToMain(String mode) {
        Intent intent = new Intent(this, MainActivity.class);
        intent.putExtra("MODE", mode);
        startActivity(intent);
    }
}

4. Manifest 設定啟動 Activity

<activity
    android:name=".StartActivity"
    android:exported="true"
    android:theme="@style/Theme.AppCompat.NoActionBar">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

5. 重置選擇(可選)

在設定頁提供「重新選擇模式」按鈕:

prefs.edit().remove(KEY_MODE).apply();
// 或 prefs.edit().clear().apply();
recreate(); // 重啟 Activity 顯示選擇頁

進階:使用 DialogFragment 替代獨立 Activity

若不想增加一個 Activity,可在 MainActivity.onCreate 中判斷並彈出 DialogFragment,邏輯相同,體驗更輕量。

結語

透過 ImageButton + SharedPreferences 即可快速實現啟動雙模式選擇,代碼簡潔且符合 Android 設計規範。後續可擴展為多模式、A/B 測試框架等。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved