試試這個
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();
}
}
這是一個很好的開始!感謝那小小的代碼塊。現在,我只需要弄清楚如何在按鈕單擊而不是定時器上完成這些操作=) – Andrew 2012-04-02 04:34:02
此處計時器僅用於啓動畫面延遲,您可以將其設置爲按鈕單擊,並存儲任何鍵對於按鈕點擊並存儲在sharedpref中,並在下一次您可以使用該值來顯示哪個屏幕。 – wolverine 2012-04-02 04:39:53
使用該代碼工作了一下之後,我能夠得到正是我想要的。非常感謝你! – Andrew 2012-04-02 05:05:28