目前我的代码加载了TabWidget
个标签。第一个标签指向免责声明页面,该页面还从编辑文本框中获取用户名并将其内部存储在手机上。我希望有一个if条件,检查用户名是否存储在手机上,如果是,则向用户显示与原始免责声明页面不同的“谢谢”页面。我相信我的代码不起作用,因为它在onCreate函数下并且没有刷新...
public class TabWidgetActivity extends TabActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Resources res = getResources(); // Resource object to get Drawables
TabHost tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Reusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
// Create an Intent to launch an Activity for the tab (to be reused)
String username = null;
String pulledUsername = getPreferences(MODE_PRIVATE).getString("username",username);
// ******* MAIN PART I'M HAVING TROUBLE WITH ********************************
if (pulledUsername != null){
intent = new Intent().setClass(this, HomeActivity.class);
}
else {
intent = new Intent().setClass(this, DisclaimerActivity.class);
}
// ^^^^^^^^^^^^^^^ MAIN PART I'M HAVING TROUBLE WITH ^^^^^^^^^^^^^^^^^^^^^^
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("home").setIndicator("Home",
res.getDrawable(R.drawable.ic_tab_home))
.setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this, ShowMapActivity.class);
spec = tabHost.newTabSpec("map").setIndicator("Map",
res.getDrawable(R.drawable.ic_tab_map))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, SurveyActivity.class);
spec = tabHost.newTabSpec("survey").setIndicator("Survey",
res.getDrawable(R.drawable.ic_tab_web))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, AnalysisActivity.class);
spec = tabHost.newTabSpec("analysis").setIndicator("Analysis",
res.getDrawable(R.drawable.ic_tab_analysis))
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(2);
}
}
答案 0 :(得分:0)
你能更清楚“不工作”吗?
您是否在此活动的首选项中设置了用户名?如果没有,那么您应该使用getSharedPreferences()
,因为首选项存储在每个活动的基础上。
在免责声明活动中编写用户名
SharedPreferences settings = getSharedPreferences("com.blah.application", MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.putString("username", username);
// Commit the edit!
editor.apply();
在其他活动中提取用户名时:
pulledUsername = getSharedPreferences("com.blah.application", MODE_PRIVATE).getString("username",null)