Android-活动中的Tab内容丢失

时间:2012-02-13 20:01:44

标签: android android-tabhost

   public class SampleTabActivity extends TabActivity implements OnClickListener{static TabHost tabHost;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    tabHost = getTabHost();  // The activity TabHost
    TabHost.TabSpec spec;  // Reusable TabSpec for each tab
    Intent intent;  // Reusable Intent for each tab
      //this is SampleTabActivity.java file
    intent = new Intent().setClass(this, TabOne.class);
    spec = tabHost.newTabSpec("tabOne");  
    spec.setContent(intent);  
    spec.setIndicator("Tab One");  
    tabHost.addTab(spec);
    tabHost.getTabWidget().getChildAt(0).getLayoutParams().height = 40;
    LinearLayout ll = (LinearLayout) tabHost.getChildAt(0);
    android.widget.TabWidget tw = (android.widget.TabWidget) ll.getChildAt(0);
    RelativeLayout rllf = (RelativeLayout) tw.getChildAt(0);
    TextView lf = (TextView) rllf.getChildAt(1);
    lf.setTextSize(20);

    intent = new Intent().setClass(this, TabTwo.class);
    spec = tabHost.newTabSpec("tabTwo");  
    spec.setContent(intent);  
    spec.setIndicator("Tab Two");
    tabHost.addTab(spec);
    tabHost.getTabWidget().getChildAt(1).getLayoutParams().height = 40;
    RelativeLayout rlrf = (RelativeLayout) tw.getChildAt(1);
    TextView rf = (TextView) rlrf.getChildAt(1);
    rf.setTextSize(20);

    intent = new Intent().setClass(this, TabThree.class);
    spec = tabHost.newTabSpec("tabThree");  
    spec.setContent(intent);  
    spec.setIndicator("Tab Three");
    tabHost.addTab(spec);
    tabHost.getTabWidget().getChildAt(2).getLayoutParams().height = 40;
    RelativeLayout rlrp = (RelativeLayout) tw.getChildAt(2);
    TextView rp = (TextView) rlrp.getChildAt(1);
    rp.setTextSize(20);
    tabHost.setCurrentTab(0);
}}





public class TabOne extends ActivityGroup implements OnClickListener{   @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.tabone);
  TextView tv = new TextView(this);
  tv.setText("This is tab One");
  //this is TabOne.java file
  Button button = (Button)findViewById(R.id.button1);
  button.setOnClickListener(this);    
 }

@Override
public void onClick(View v) {   
    Intent intent = new Intent(this, TabTwo.class);         
    replaceContentView("TabTwo", intent);
    setTab(1);
}

public void replaceContentView(String id, Intent newIntent) {     
    View view = getLocalActivityManager().startActivity(id,newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView(); 
    this.setContentView(view);

    }

public void setTab(int index){    
    SampleTabActivityActivity ParentActivity;    
    ParentActivity = (SampleTabActivityActivity) this.getParent();    
    ParentActivity.getTabHost().setCurrentTab(index); 
    } }




public class TabTwo extends ActivityGroup implements OnClickListener{@Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  TextView tv = new TextView(this);
  tv.setText("This is tab Two");
  setContentView(tv);
 }}


public class TabThree extends ActivityGroup implements OnClickListener{@Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  TextView tv = new TextView(this);
  tv.setText("This is tab Three");
  setContentView(R.layout.tabone);
 }}

在这里,当我点击TabOne中的继续按钮...它转到选项卡Two ..并显示文本“这是选项卡二”....但是当我通过单击TabOne选择tabOne时......内容是丢失...

我再也看不到“继续”按钮...相反,我看到“这是标签二”......

请检查代码。 请一些帮助...需要做什么......我哪里出错... ????

2 个答案:

答案 0 :(得分:1)

您的代码应该在onResume()方法中。如果activity不存在,将调用onCreate(),后续调用将由onResume()方法处理。请参阅此link中的活动生命周期。

答案 1 :(得分:0)

您的onClick方法是硬编码的,每次单击按钮时它都会转到第二个选项卡。