これは、最初のタブに入力したものが何であれ、2番目のタブに表示される小さなサンプルです。
メインクラス
public class CheckkActivity 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();
TabHost tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
intent = new Intent().setClass(this, NewActivity.class);
spec = tabHost.newTabSpec("first").setIndicator("First")
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, SecondActivity.class);
spec = tabHost.newTabSpec("second").setIndicator("Second")
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(0);
}
}
NewActivity
public class NewActivity extends Activity{
EditText get;
Button save;
SharedPreferences sharedPreferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.next);
get=(EditText)findViewById(R.id.next);
save=(Button)findViewById(R.id.button1);
save.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if(get.getText().toString().equalsIgnoreCase("")){
Toast.makeText(getApplicationContext(), "enter something", Toast.LENGTH_SHORT).show();
}else{
sharedPreferences=PreferenceManager.getDefaultSharedPreferences(NewActivity.this);
Editor editor1 = sharedPreferences.edit();
editor1.remove("answer");
editor1.commit();
sharedPreferences=PreferenceManager.getDefaultSharedPreferences(NewActivity.this);
Editor editor = sharedPreferences.edit();
Log.i("set value",""+get.getText().toString());
editor.putString("answer",get.getText().toString());
editor.commit();}
}
});
}
}
SecondActivity
public class SecondActivity extends Activity{
TextView set;
SharedPreferences sharedPreferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
set=(TextView)findViewById(R.id.second);
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(SecondActivity.this);
String answer= sharedPreferences.getString("answer", "");
Log.v("get value",""+answer);
if(answer.equalsIgnoreCase(null)){
set.setText("nothing to display");
}else{
set.setText(answer);
}
super.onResume();
}
}