我有一个Android应用程序,我有两个选项卡。我希望每当选项卡发生变化时。选择的选项卡应从头开始加载。我不希望在更改标签时显示任何以前的数据... 我尝试使用制表符更改侦听器但它没用。 请建议我能做些什么。我是Android开发的新手
这是我的代码
解决
package com.example.pms;
import android.app.AlertDialog;
import android.app.TabActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TextView;
import android.widget.TabHost.TabSpec;
import android.widget.Toast;
public class TabControl extends TabActivity
{
public static TabControl mTabControl;
public static TextView textView;
public static TabHost tabHost ;
final Context context = this;
@SuppressWarnings("deprecation")
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
Resources resources = getResources();
TabHost tabHost = getTabHost();
Bundle extras = getIntent().getExtras();
String strEmployeeID="";
TabSpec photospec = tabHost.newTabSpec("Hourly");
// setting Title and Icon for the Tab
photospec.setIndicator("Hourly Entry");
if (extras != null)
{
String value = extras.getString("new_variable_name");
// Toast.makeText(getBaseContext(), value, Toast.LENGTH_LONG).show();
strEmployeeID = value;
}
Intent photosIntent = new Intent(getApplicationContext(), HourlyEntry.class);
photosIntent.putExtra("new_variable_name",strEmployeeID);
photospec.setContent(photosIntent);
TabSpec photospec1 = tabHost.newTabSpec("Leave");
// setting Title and Icon for the Tab
photospec1.setIndicator("Leave App");
if (extras != null)
{
String value = extras.getString("new_variable_name");
// Toast.makeText(getBaseContext(), value, Toast.LENGTH_LONG).show();
strEmployeeID = value;
}
Intent photosIntent1 = new Intent(getApplicationContext(), LeaveApp.class);
photosIntent1.putExtra("new_variable_name",strEmployeeID);
photospec1.setContent(photosIntent1);
tabHost.addTab(photospec);
tabHost.addTab(photospec1);
tabHost.setCurrentTab(0);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
AlertDialog.Builder alertbox = new AlertDialog.Builder(TabControl.this);
alertbox.setTitle("You Want To Sign Out ?");
alertbox.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
// finish used for destroyed activity
signout();
}
});
alertbox.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
// Nothing will be happened when clicked on no button
// of Dialog
}
});
alertbox.show();
}
return super.onKeyDown(keyCode, event);
}
public void signout()
{
Intent i = new Intent(getApplicationContext(), MainActivity.class);
startActivity(i);
}
}
答案 0 :(得分:1)
定义 photospec,photospec1 全球范围内:
public class TabControl extends TabActivity
{
TabSpec photospec,photospec1;
.........
..........
}
或移动此代码
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
public void onTabChanged(String tabId) {
if(photospec.equals(tabId)) {
//destroy earth
}
if(photospec1.equals(tabId)) {
//destroy mars
}
}
});
onCreate(.......)