public class MainActivity extends ActionBarActivity {
TextView show,setting;
Dao dao = Dao.getInstance();
int i = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initListener();
}
boolean flg=false;
private void initListener() {
setting.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this,SettingActivity.class);
startActivity(intent);
if (flg==true){
flg=false;
}else {
flg=true;
}
setting.setSelected(flg);
final Timer timer = new Timer();
timer .schedule(new TimerTask() {
@Override
public void run() {
i++;
Log.e("=============", "youzhadan" + i);
if (i==5){
timer.cancel();
}
}
}, 0, 10000);
}
});
}
private void initView() {
show= (TextView) findViewById(R.id.show);
String name =dao.getName();
show.setText(name);
setting= (TextView) findViewById(R.id.setting);
}
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
if(event.getKeyCode()==KeyEvent.KEYCODE_BACK){
System.exit(0);
}
return super.dispatchKeyEvent(event);
}
@Override
public void onBackPressed() {
super.onBackPressed();
}
}
SettingActivity
public class SettingActivity extends ActionBarActivity{
TextView open,close,back;
Dao dao= Dao.getInstance();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setting);
initView();
initListener();
Log.e("=====", dao.toString());
}
private void initListener() {
open.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dao.setName("zhangsan");
}
});
close.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dao.setName("lisi");
}
});
back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
}
});
}
private void initView() {
open = (TextView) findViewById(R.id.open);
close = (TextView) findViewById(R.id.close);
back = (TextView) findViewById(R.id.back);
}
}
Dao
public class Dao implements Serializable{
public static Dao instance;
public static Dao getInstance(){
if(instance==null){
instance = new Dao();
}
return instance;
}
String Name;
char Sex;
int age;
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public char getSex() {
return Sex;
}
public void setSex(char sex) {
Sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
MainActivitypublic class MainActivity extends ActionBarActivity { TextView show,setting; Dao dao = Dao.getInstance(); int i = 0; @Override protected void onCreate(Bundle savedIn