2012-04-02 31 views
0

我正在對我的應用程序進行一些更新,我希望允許用戶從兩個不同(儘管非常相似)的UI中進行選擇。我已經設置了兩種不同的樣式,但我仍然對構建應用程序非常陌生。啓動畫面根據喜好啓動活動?

基本上這裏就是我想發生什麼:

  • 在第一次啓動顯示啓動畫面我已經有兩個按鈕和指示創建做什麼(選擇他們想要哪個UI有)和存儲他們選擇的地方

  • 在任何推出他們所提出後,他們的決定,並將它們發送到他們所選擇的用戶界面和不顯示啓動畫面

  • 有一個選項(在某處的選項s菜單)允許它們更改應用程序的UI

我唯一遇到的問題是啓動屏幕的java。如果有人能夠幫助我,那麼我應該能夠自己處理剩下的問題。

在此先感謝!

回答

1

我有一個閃屏的解決方案,只有在第一次使用共享首選項運行應用程序時纔會出現。 試試這個,

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); 
    } 
} 
+0

這是一個很好的開始!感謝那小小的代碼塊。現在,我只需要弄清楚如何在按鈕單擊而不是定時器上完成這些操作=) – Andrew 2012-04-02 04:34:02

+0

此處計時器僅用於啓動畫面延遲,您可以將其設置爲按鈕單擊,並存儲任何鍵對於按鈕點擊並存儲在sharedpref中,並在下一次您可以使用該值來顯示哪個屏幕。 – wolverine 2012-04-02 04:39:53

+0

使用該代碼工作了一下之後,我能夠得到正是我想要的。非常感謝你! – Andrew 2012-04-02 05:05:28

0

試試這個

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