开发应用,需要判断用户是不是第一次进入程序,首次进入需要注册,不是第一次进入,直接跳转到第二个界面
思路:将标志位存储起来,进入应用的时候读取判断,如果为默认值,则首次进入,如果已经设置过了,则不为首次进入。
上代码:
SharedPreferences setting;
final String INITIALIZED = "initialized";
Boolean user_first;
String phonenum1=null;
String phonenum2=null;
public void signUp(View view) {
phonenum1 = ((EditText) findViewById(R.id.et_phonenum1))
.getText().toString();
phonenum2= ((EditText) findViewById(R.id.et_phonenum2))
.getText().toString();
//whether input is phone number
if (!Patterns.PHONE.matcher(phonenum1).matches()) {
Toast.makeText(this, R.string.invalid_phonenum, Toast.LENGTH_SHORT).show();
return;
}
//whether the two numbers are the same
if (!phonenum1.equals(((EditText) findViewById(R.id.et_phonenum2))
.getText().toString())) {
Toast.makeText(this, R.string.inconsistent_phonenum,
Toast.LENGTH_SHORT).show();
return;
}
Intent intent=new Intent(this, MainActivity.class);
intent.putExtra("PHONE_NUM", phonenum2);
startActivity(intent);
finish();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);//move the title
setting=getSharedPreferences("setting", 0);打开Preferences,名称为setting,如果存在则打开它,否则创建新的Preferences
user_first = setting.getBoolean("FIRST",true);//取得相应的值,如果没有该值,说明还未写入,用true作为默认值
if(user_first){
//第一次登录,正常加载
setContentView(R.layout.activity_sign_up);
}else{
//如果不是第一次登录,直接跳转到下一个界面
Intent intent=new Intent(this, MainActivity.class);
startActivity(intent);
finish();
}
setting.edit().putBoolean("FIRST", false).commit();
}