存储用户选择,该选择存储自身以供在 Android 应用程序中重新加载时使用

发布于 2024-09-12 01:36:57 字数 971 浏览 2 评论 0原文

我构建了一个对话框,要求用户从应用程序首次打开时提供的列表中选择一个城市。该对话框工作正常,但是我想存储用户的选择,以便当第二次打开应用程序时,它会检查用户之前是否已经做出选择。如果有,它不会显示对话框并将城市变量定义为他们之前选择的首选项。显然,如果他们之前没有做出选择(因为这是他们第一次打开应用程序,或者由于某种原因应用程序无法读取存储的首选项),则会显示该对话框。

这是我的对话框,以防万一这有帮助:

final CharSequence[] CityChoice = {"Austin", "Dallas/Fort Worth", "Houston", "San Antonio"};
 AlertDialog.Builder alt_bld = new AlertDialog.Builder(this);
 alt_bld.setIcon(R.drawable.icon);
 alt_bld.setTitle("Select your city");
 alt_bld.setSingleChoiceItems(CityChoice, -1, new DialogInterface.OnClickListener() {
  public void onClick(DialogInterface dialog, int item) {
   Toast.makeText(getApplicationContext(), "Your city is now set to "+CityChoice[item]+". To change this, go to Menu -> Preferences.", Toast.LENGTH_LONG).show();
   dialog.dismiss();
              }
          });
AlertDialog alert = alt_bld.create();
alert.show();

编辑:哦,顺便说一句,虽然我很快就学会了 android 编程(至少我认为我是哈哈),但我必须承认我对此很陌生。因此,您的回复越详细越好。提前非常感谢。

I've built a dialog that asks the user to pick a city from the list provided when the application first opens. The dialog works perfectly, however I want to store the user's choice so that when the app is opened a second time, it checks to see if the user has already made a selection previously. If they have, it doesn't display the dialog and defines the city variable as their previously chosen preference. And obviously, if they haven't made a selection previously (because its their first time opening the app or for some reason the app couldn't read the stored preference), it displays the dialog.

Here's my dialog in case this helps:

final CharSequence[] CityChoice = {"Austin", "Dallas/Fort Worth", "Houston", "San Antonio"};
 AlertDialog.Builder alt_bld = new AlertDialog.Builder(this);
 alt_bld.setIcon(R.drawable.icon);
 alt_bld.setTitle("Select your city");
 alt_bld.setSingleChoiceItems(CityChoice, -1, new DialogInterface.OnClickListener() {
  public void onClick(DialogInterface dialog, int item) {
   Toast.makeText(getApplicationContext(), "Your city is now set to "+CityChoice[item]+". To change this, go to Menu -> Preferences.", Toast.LENGTH_LONG).show();
   dialog.dismiss();
              }
          });
AlertDialog alert = alt_bld.create();
alert.show();

edit: Oh, and by the way, although I'm picking up android programming fairly quickly (at least I think I am haha) I have to admit I'm quite new to it. So the more detailed your response the better. Thanks a ton in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

画▽骨i 2024-09-19 01:36:57

您可以使用 SharedPreferences 来存储和检索设置,这非常简单,如下所示 http://developer.android.com/guide/topics/data/data-storage.html

根据设置启动对话框应该很简单。然后可以将该设置保存在对话框的侦听器中。

You could use SharedPreferences to store and retrieve the setting, which is pretty straightforward as shown here http://developer.android.com/guide/topics/data/data-storage.html

Launching the dialog based on the setting should be trivial. The setting can then be saved in the dialog's listener.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文