0
目前我的代碼加載了帶有4個選項卡的TabWidget
。第一個標籤指向一個免責聲明頁面,該頁面還從edittext框中獲取用戶名,並將其存儲在手機內部。我希望有一個if條件來檢查用戶名是否存儲在電話中,如果是,則向用戶顯示與原始免責聲明頁面不同的「謝謝」頁面。我相信我的代碼不能正常工作,因爲它的功能的onCreate下,不爽快......如何通過使用if語句使TabWidget改變意圖
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);
}
}
聽起來像這可能是問題...我寫它的方式在免責聲明活動是: getPreferences(MODE_PRIVATE).edit()。putString(「username」,username).commit(); 我應該如何重寫getSharedPreferences?簡單地更改名稱會拋出一個錯誤:ContextWrapper類型中的方法getSharedPreferences(String,int)不適用於參數(int) – ZeroSkittles 2012-03-22 22:48:36
示例代碼添加 – DavidBriggs 2012-03-22 23:24:27
肯定是問題的一部分,現在唯一的問題是onCreate永遠不會再次呼籲。我希望它在點擊按鈕後執行檢查。 – ZeroSkittles 2012-04-03 22:01:07