用户选择哪个活动首先开始?

时间:2013-11-06 08:02:01

标签: android android-activity

我需要帮助,请用户选择首先启动哪个活动....

我有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开头 我如何设置由用户选择哪一个开始?

提前致谢..

2 个答案:

答案 0 :(得分:3)

创建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();
    }
}

答案 1 :(得分:0)

创建临时活动。在此活动中,用户可以选择活动名称。保存选定的活动名称(使用此参数“活动”)(例如; FirstActivity,SecondActivity,ThirdActivity)。

getSharedPreferencesStringValue =&gt;编写此函数

Algorith 并在临时活动中控制它(在onCreate函数处。)。

1 - 如果有一个名称为“activity”的值,则再次控制它。

 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

致以最诚挚的问候,