Android简单登入注册界面的实现

LoginAvtivity如下:

   主要是sharedpreference和文本框.getText对于各个界面的密码用户名的保存和使用,还有就是状态的保存,第一次注册过就不能再注册.
   其中我发现在一个活动中使用TextView,****比如TextView tv=(TextView)findViewById(R.id.tv);中即使你将TextView tv设置成public TextView tv;
   在其他活动中也无法使用,这是因为TextView是在onCreate()方法中定义的,局部变量设置public,private之类的都是无效的.此时只能依赖preference。


   public class LoginActivity extends Activity  implements OnClickListener{
private Button loginButton;
private Button registerButton;
public EditText username;
private EditText password;

// public SharedPreferences sp;
private String aString;
private String bString;
private SharedPreferences preferences;
private Editor editor;
private Boolean ab;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_login);
    preferences = getSharedPreferences("config", MODE_PRIVATE);
    editor = preferences.edit();
    loginButton = (Button) findViewById(R.id.btn_login);
    registerButton = (Button) findViewById(R.id.btn_register);
    username = (EditText) findViewById(R.id.username);
    password = (EditText) findViewById(R.id.password);
    loginButton.setOnClickListener(this);
    registerButton.setOnClickListener(this);
    ab = preferences.getBoolean("ok", false);
    aString = preferences.getString("YY", "");
    bString = preferences.getString("LL", "");

}

public void login(){
String aa=username.getText().toString();
String bb=password.getText().toString();
if (aa.equals(aString)&& bb.equals(bString)) {
Intent intent2=new Intent(LoginActivity.this,MainActivity.class);
startActivity(intent2);

}
else {



Intent intent = new Intent(LoginActivity.this,
        AgainLoginActivity.class);
        startActivity(intent);

}

}

public void register(){
    if (ab) {
        Toast.makeText(getApplicationContext(), "你已经注册过了", 0).show();

    }else {



    Intent intent = new Intent(LoginActivity.this,
            RegisterActivity.class);
    startActivity(intent);
    editor.putBoolean("ok",true);
    editor.commit();
    finish();

    }

}

@Override
public void onClick(View v) {
    switch (v.getId()) {
    case R.id.btn_login:
            login();
        break;
    case R.id.btn_register:
            register();
        break;

    default:
        break;
    }

}

}

register.java如下:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_register);


    username = (EditText) findViewById(R.id.username);
    password = (EditText) findViewById(R.id.password);
    confirm_password = (EditText) findViewById(R.id.confirm_password);
    registerButton = (Button) findViewById(R.id.btn_register);

    registerButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
             yonghuming=username.getText().toString();
             mima=password.getText().toString();
            querenmima=confirm_password.getText().toString();
            sp=getSharedPreferences("config", MODE_PRIVATE);
              Editor editor=sp.edit();
            editor.putString("YY", yonghuming);
            editor.putString("LL", mima);
            editor.commit();
            if (!TextUtils.isEmpty(yonghuming)) {


            if (mima.equals(querenmima)) {
                register();
            }
            else{
                Toast.makeText(getApplicationContext(), "两次密码不一致", Toast.LENGTH_LONG).show();               }


        }
            else {
                Toast.makeText(getApplicationContext(), "用户名为空", 0).show();
            }
        }
    });

}

public void register() {
    Intent intent = new Intent(RegisterActivity.this,AgainLoginActivity.class);
    startActivity(intent);
    finish();

}

}

AgainLoginAtivity.java如下:

public class AgainLoginActivity extends Activity implements OnClickListener{
private Button loginButton;
private Button registerButton;
public EditText username2;
public EditText password2;
public String mima2;
public String yonghuming2;
SharedPreferences sp;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_login2_again);
    username2 = (EditText) findViewById(R.id.username2);
    password2 = (EditText) findViewById(R.id.password2);

    loginButton = (Button) findViewById(R.id.btn_login);



    loginButton.setOnClickListener(this);


}

public void login(){
mima2=password2.getText().toString();
yonghuming2=username2.getText().toString();
SharedPreferences preferences=getSharedPreferences(“config”, MODE_PRIVATE);
String name=preferences.getString(“YY”, “”);
String mimaa=preferences.getString(“LL”,”“);

if (yonghuming2.equals(name)) {
    if (mima2.equals(mimaa)) {
        Intent intent = new Intent(AgainLoginActivity.this,
                MainActivity.class);
        startActivity(intent);
        finish();

    }
    else {
        Toast.makeText(getApplicationContext(), "密码不同", Toast.LENGTH_SHORT).show();

    }

}
else {
    Toast.makeText(getApplicationContext(), "用户名不同", Toast.LENGTH_SHORT).show();
}

}

@Override
public void onClick(View v) {
    switch (v.getId()) {
    case R.id.btn_login:

            login();
        break;

    default:
        break;
    }

}

}

你可能感兴趣的:(Android)