【问题标题】:Splash Screen start activity depending on preference?启动屏幕根据偏好启动活动?
【发布时间】:2012-04-15 18:41:24
【问题描述】:

我正在对我的应用程序进行更多改造,并希望允许用户从两个不同(尽管非常相似)的 UI 中进行选择。我已经设置了两种不同的样式,但我对构建应用程序还是很陌生。

基本上这就是我想要发生的事情:

  • 在第一次启动时显示我创建的初始屏幕,其中包含两个按钮和操作说明(选择他们想要的 UI)并将他们的选择存储在某处

  • 在他们做出决定后的任何启动中,将他们发送到他们选择的 UI 并且不显示初始屏幕

  • 有一个选项(在选项菜单中的某处)允许他们更改应用程序的 UI

我唯一遇到的问题是用于启动画面的 java。如果有人能帮我解决这个问题,那么我应该能够自己完成其余的工作。

提前致谢!

【问题讨论】:

    标签: android splash-screen preference


    【解决方案1】:

    查看此链接中的答案以获取示例闪屏: Android: Splash Screen re-opens app to second page even if app is quit during splash screen 不要使用线程来超时,而是等待用户单击布局中的两个按钮之一并将其保存在共享首选项中。

    【讨论】:

      【解决方案2】:

      我有一个解决方案,通过使用共享首选项,启动屏幕仅在第一次运行应用程序时出现。 试试这个,

      public class Splash extends Activity {
          private long splashDelay = 1500;
          int counter;
          SharedPreferences app_preferences;
      
          @Override
          public void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.splash);
      
              app_preferences = PreferenceManager.getDefaultSharedPreferences(this);
              counter = app_preferences.getInt("counter", 1);
              System.out.println("count is..." + counter);
      
              TimerTask task = new TimerTask() {
                  @Override
                  public void run() {
                      finish();
                      if (counter == 1) {
                          Intent in = new Intent(Splash.this, Yourclass1.class);
                          startActivity(in);
                      } else {
                          Intent intent = new Intent().setClass(Splash.this, Yourclass2.class);
                          startActivity(hackbookIntent);
                      }
                      SharedPreferences.Editor editor = app_preferences.edit();
                      editor.putInt("counter", +(counter + 1));
                      editor.commit();
      
                  }
      
              };
      
              Timer timer = new Timer();
              timer.schedule(task, splashDelay);
          }
      }
      

      【讨论】:

      • 这是一个很好的开始!感谢那一小段代码。现在我只需要弄清楚如何在按钮点击而不是计时器上做一些事情 =)
      • 这里的定时器只用于闪屏延迟,你可以将它设置为按钮点击,并将按钮点击的任意键存储在sharedpref中,下次你可以使用要显示哪个屏幕的值..
      • 在处理了一些代码之后,我能够得到我想要的东西。非常感谢!
      【解决方案3】:

      试试这个

      public class TestActivity extends Activity {
      private SharedPreferences sharedPrefs;
      private Editor prefsEditor;
      private static final String APP_SHARED_PREFS = "com.demo.test.Login_preferences";
      private static final String APP_CHOICE = "Choice";
      private static final String APP_DESIGN = "Design";
      private static final String APP_DEVEL = "Develop";
      
      /** Called when the activity is first created. */
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
      
          sharedPrefs = getSharedPreferences(APP_SHARED_PREFS,
                  Context.MODE_PRIVATE);
          String choice = getValueforKey(APP_CHOICE);
          if(choice.length() == 0){
              setContentView(R.layout.main);
          Button design = (Button) findViewById(R.id.des);
          Button dev = (Button) findViewById(R.id.dev);
      
          design.setOnClickListener(new OnClickListener() {
      
              @Override
              public void onClick(View v) {
                  // TODO Auto-generated method stub
                  saveKey(APP_CHOICE,APP_DESIGN);
                  Intent intent = new Intent(TestActivity.this , Design.class);
                  startActivity(intent);
                  finish();
              }
          });
      
          dev.setOnClickListener(new OnClickListener() {
      
              @Override
              public void onClick(View v) {
                  // TODO Auto-generated method stub
                  saveKey(APP_CHOICE,APP_DEVEL);
                  Intent intent = new Intent(TestActivity.this , Develop.class);
                  startActivity(intent);
                  finish();
              }
          });
          }else if(choice.equals(APP_DESIGN)){
              Intent intent = new Intent(this , Design.class);
              startActivity(intent);
          }else if(choice.equals(APP_DEVEL)){
              Intent intent = new Intent(this , Develop.class);
              startActivity(intent);
          }
      
      
      }
      
      public String getValueforKey(String Key)
      {
          this.sharedPrefs =getSharedPreferences(APP_SHARED_PREFS,
                  Context.MODE_PRIVATE);
          return sharedPrefs.getString(Key, "");
      }
      
      public void saveKey(String Key, String value) {
          this.sharedPrefs = getSharedPreferences(APP_SHARED_PREFS,
                  Context.MODE_PRIVATE);
           this.prefsEditor = sharedPrefs.edit();
          prefsEditor.putString(Key, value);
          prefsEditor.commit();
      }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-05
        • 1970-01-01
        • 1970-01-01
        • 2023-03-18
        相关资源
        最近更新 更多