Android判断是否首次登录

开发应用,需要判断用户是不是第一次进入程序,首次进入需要注册,不是第一次进入,直接跳转到第二个界面

思路:将标志位存储起来,进入应用的时候读取判断,如果为默认值,则首次进入,如果已经设置过了,则不为首次进入。

上代码:

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();


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值