第一次登陆记录数据,二次登陆后直接进入指定界面,常用于二次登陆之后免登陆
private TextView First_tv;
private SharedPreferences sp;
private Editor editor;
private String test;
private Intent intent = new Intent();
private Button read;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/***
* 这句代码是指创建一个文件夹,并存在里面,读取和写入都需要,并且这句要写在加载视图的上面,否则能看见第一次加载的*
* **/
SharedPreferences sp = getSharedPreferences("phone", Activity.MODE_PRIVATE);
test= sp.getString("test", "");
Toast.makeText(MainActivity.this, ""+test, 0).show();
if(test.equals("test")&&test!=null){
intent.setClass(MainActivity.this, SecondActivity.class);
startActivity(intent);
}
setContentView(R.layout.activity_main);
First_tv = (TextView) findViewById(R.id.First_tv);
read = (Button) findViewById(R.id.read);
read.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
// sp = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
SharedPreferences sp = getSharedPreferences("phone", Activity.MODE_PRIVATE);
editor = sp.edit();
editor.putString("test", "test");
editor.commit();
}
});
}
第一次需要点击事件将数据放入到sp中,SharedPreferences sp = getSharedPreferences(“phone”, Activity.MODE_PRIVATE);这句是创建一个Phone的文件夹,读写都需要。二次进入之后,会直接跳入SecondActivity
private Button SecondBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
SecondBtn = (Button) findViewById(R.id.SecondBtn);
SecondBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "SecondActivity", 0).show();
}
});
}
@Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
finish();
}