启动画面根据喜好启动活动?
问题描述:
我正在对我的应用程序进行一些更新,我希望允许用户从两个不同(尽管非常相似)的UI中进行选择。我已经设置了两种不同的样式,但我仍然对构建应用程序非常陌生。启动画面根据喜好启动活动?
基本上这里就是我想发生什么:
在第一次启动显示启动画面我已经有两个按钮和指示创建做什么(选择他们想要哪个UI有)和存储他们选择的地方
在任何推出他们所提出后,他们的决定,并将它们发送到他们所选择的用户界面和不显示启动画面
有一个选项(在某处的选项s菜单)允许它们更改应用程序的UI
我唯一遇到的问题是启动屏幕的java。如果有人能够帮助我,那么我应该能够自己处理剩下的问题。
在此先感谢!
答
我有一个闪屏的解决方案,只有在第一次使用共享首选项运行应用程序时才会出现。 试试这个,
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);
}
}
答
检查这个链接一个样本闪屏回答: Android: Splash Screen re-opens app to second page even if app is quit during splash screen 而不是使用一个线程超时,等待用户点击您的布局两个按钮中的一个,并将其保存在共享偏好。
答
试试这个
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