1

ユーザーがどのアクティビティを最初に起動するかを選択してください....

3 つのアクティビティと設定アクティビティがあります。最初に起動して保存するアクティビティをユーザーに選択してもらいます。

誰かが正しいコードや例を教えてくれれば幸いです...

<activity
    android:label="@string/app_name"
    android:screenOrientation="portrait"
    android:name="com.example.Test" 
    android:theme="@style/Theme.FullScreen">
    <intent-filter >
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

アプリを起動するときは、常に Test.class で開始します。ユーザーが設定する方法はどれですか?

前もって感謝します..

4

2 に答える 2

4

Activityアプリのメイン エントリ ポイントとして を作成します。このアクティビティは、ユーザーがデフォルトのアクティビティを選択したかどうかをチェックします。彼が持っている場合、このアクティビティはそのアクティビティを開始し、それ自体を閉じます。それ以外の場合、ユーザーはデフォルトのアクティビティを選択するように求められます。

サンプルコード (要件に合わせていくつかの変更を加える必要があります):

public class MainActivity extends Activity {

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

        final SharedPreferences sharedPref = getPreferences(MODE_PRIVATE);
        int choice = sharedPref.getInt("default_activity", -1);

        if (choice == -1) {
            // show the option to choose the default activity to the user
            // e.g. dialog with list, then save the corresponding choice to
            // shared preference
            String[] activities = { "Activity 1", "Activity 2", "Activity 3" };

            AlertDialog.Builder builder = new Builder(this);
            builder.setAdapter(
                new ArrayAdapter<String>(this,
                    android.R.layout.simple_list_item_1, activities),
                new OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    SharedPreferences.Editor editor = sharedPref.edit();
                    editor.putInt("default_activity", which);
                    editor.commit();
                    launchActivity(which);
                }
                }).show();
        } else {
            // start the activity and close this activity
            launchActivity(choice);
        }
    }

    private void launchActivity(int choice) {
        switch (choice) {
        case 0:
            startActivity(new Intent(this, Activity1.class));
            break;
        case 1:
            startActivity(new Intent(this, Activity2.class));
            break;
        case 2:
            startActivity(new Intent(this, Activity3.class));
            break;
        }
        finish();
    }
}
于 2013-11-06T08:31:10.693 に答える
0

一時活動を作成します。このアクティビティでは、ユーザーはアクティビティ名を選択できます。選択したアクティビティ名を保存します (このパラメーター「アクティビティ」を使用) (例: FirstActivity、SecondActivity、ThirdActivity)。

getSharedPreferencesStringValue =>この関数を書く

Algorith そしてtempアクティビティ(onCreate関数で)でそれを制御します。

1-「活動」という名前の値がある場合は、再びそれを制御します。

 1.1-If getSharedPreferencesStringValue("activity").equals("FirstActivity")

  -Redirect FirstActivity
 1.2-If getSharedPreferencesStringValue("activity").equals("SecondActivity")

  -Redirect SecondActivity
 1.2-If getSharedPreferencesStringValue("activity").equals("ThirdActivity")

  -Redirect ThirdActivity

2-「アクティビティ」という名前の値がない場合

 2.1-Open temp activity

 2.2-Show activity list

 2.3-After than user chose an activity, save it preferences

 2.4-And open that activity

よろしくお願いします、

于 2013-11-06T08:32:35.897 に答える