gpt4 book ai didi

android - 用户选择哪个 Activity 先启动?

转载 作者:行者123 更新时间:2023-11-29 16:04:56 26 4
gpt4 key购买 nike

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

我有 3 个 Activity 和设置 Activity ,我希望用户先选择一个 Activity 并保存。

如果有人指导我找到正确的代码或示例,我将不胜感激......

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

提前致谢..

最佳答案

创建一个 Activity 作为您应用的主要入口点。此 Activity 将检查用户是否选择了默认 Activity 。如果他有,则此 Activity 将启动该 Activity 并自行关闭。否则,它会提示用户选择默认 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();
}
}

关于android - 用户选择哪个 Activity 先启动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19806801/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com