【问题标题】:How to make TabWidget change Intents by using if statement如何使用 if 语句使 TabWidget 更改 Intents
【发布时间】:2012-03-22 22:15:02
【问题描述】:

目前我的代码加载了一个带有 4 个标签的 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);
     }
    }

【问题讨论】:

    标签: java android android-intent android-widget tabwidget


    【解决方案1】:

    您能否更清楚地说明什么“不起作用”?

    您是否在此活动的首选项中设置用户名?如果不是,那么您应该使用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)
    

    【讨论】:

    • 听起来可能是问题所在......我在 DisclaimerActivity 中写的方式是:getPreferences(MODE_PRIVATE).edit().putString("username",username).commit() ;我应该如何为 getSharedPreferences 重写它?简单改名会报错:ContextWrapper类型中的getSharedPreferences(String, int)方法不适用于参数(int)
    • 肯定是问题的一部分,现在唯一的问题是 onCreate 再也不会被调用了。我希望它在单击按钮后执行检查。
    • ^解决了 startActivity(new Intent(DisclaimerActivity.this, TabWidgetActivity.class));
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-01
    相关资源
    最近更新 更多