如何在Android中跳过注册屏幕?

时间:2014-04-23 06:28:24

标签: android login android-activity

我在本地使用Sqlite数据库在Android中制作Registeration / Login模块。这是代码:

public class Registration extends Activity {

Button btnRegister;
Button btnLinkToLogin;
EditText inputFullName;
//EditText inputEmail;
EditText inputPassword;
TextView registerErrorMsg;
DbManager manager = new DbManager(this);

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_registration);

    // Assignment of UI fields to the variables
    inputFullName = (EditText) findViewById(R.id.registerName);
    //inputEmail = (EditText) findViewById(R.id.registerEmail);
    inputPassword = (EditText) findViewById(R.id.registerPassword);
    btnRegister = (Button) findViewById(R.id.btnRegister);
    // btnRegister.setOnClickListener(this);
    btnLinkToLogin = (Button) findViewById(R.id.btnLinkToLoginScreen);

    registerErrorMsg = (TextView) findViewById(R.id.register_error);
    setActionBar();

    //set OnClickListener

    btnRegister.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            final String user_name = inputFullName.getText().toString();
        //  final String email = inputEmail.getText().toString();
            final String password = inputPassword.getText().toString();

            boolean invalid = false;

            if(user_name.equals("")&&password.equals(""))
            {
                invalid = true;
                Toast.makeText(getApplicationContext(), "Please enter necessary credentials", Toast.LENGTH_SHORT).show();
            }

                        else

                            if(password.equals(""))
                            {
                                invalid = true;
                                Toast.makeText(getApplicationContext(), "Please enter your Password", Toast.LENGTH_SHORT).show();
                            }

                                else

                                    if(user_name.equals(""))
                                    {
                                        invalid = true;
                                        Toast.makeText(getApplicationContext(), "Please enter your user name", Toast.LENGTH_SHORT).show();
                                    }

                                    else
                                        if(invalid == false){
                                            String User[] = {user_name,password};
                                            // manager=new DbManager(this);)
                                            //if(manager.Login(email, password).equals(true));
                                            if (manager.Register(user_name, password))
                                            {
                                                if(user_name.equalsIgnoreCase(user_name)&&password.equalsIgnoreCase(password))
                                                {
                                                    Toast.makeText(Registration.this, "Already registered, please login", Toast.LENGTH_SHORT).show();
                                                }
                                            }

                                            else{
                                                Toast.makeText(Registration.this, "Successfully registered", Toast.LENGTH_LONG).show();
                                                manager.Insert_Values_User(User);
                                                Intent i_register = new Intent(Registration.this, login.class);
                                                startActivity(i_register);
                                                finish();
                                            }

                                        }
        }
    });

    // Link to Login Screen
    btnLinkToLogin.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {
            Intent i = new Intent(getApplicationContext(),
                    login.class);
            startActivity(i);
            // Close Registration View
            finish();
        }
    });
    }

登录类是:

public class login extends Activity {
Button btnLogin;
Button btnLinkToRegister;

EditText inputPassword;
TextView loginErrorMsg;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login); 
 // Importing all assets like buttons, text fields
  //  inputUserName = (EditText) findViewById(R.id.user_name);
    inputPassword = (EditText) findViewById(R.id.loginPassword);
    btnLogin = (Button) findViewById(R.id.btnLogin);
    btnLinkToRegister = (Button) findViewById(R.id.btnLinkToRegisterScreen);
    loginErrorMsg = (TextView) findViewById(R.id.login_error);
    setActionBar();

 // If login is successful
    btnLogin.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {
            //Check Login
           // final String user_name = inputUserName.getText().toString();
            final String password = inputPassword.getText().toString();

        DbManager users = new DbManager(login.this);
        boolean invalid = false;

        if(password.equals(""))
        {
            invalid = true;
            Toast.makeText(getApplicationContext(), "Please enter password ", Toast.LENGTH_SHORT).show();
        }


            else if(users.Login(password)){
                    if(password.equalsIgnoreCase(password))
                    {
                        Toast.makeText(login.this, "Successfully logged in", Toast.LENGTH_SHORT).show();
                         Intent i = new Intent(getApplicationContext(),
                                 MainActivity.class);
                         startActivity(i);
                         finish();
                    }

                }
             else 
                 Toast.makeText(login.this,"Invalid Password", Toast.LENGTH_SHORT).show();

             users.close();
            }
        });





 // Link to Register Screen
    btnLinkToRegister.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {
            Intent i = new Intent(getApplicationContext(),
                    Registration.class);
            startActivity(i);
            finish();
        }
    });


}

我希望只有在用户未注册的情况下才会出现注册屏幕。如果他被注册,那么应用程序应该直接打开登录屏幕。 我搜索过,但我找不到怎么做?任何有用的帮助将不胜感激。

3 个答案:

答案 0 :(得分:0)

您可以使用共享偏好..

喜欢这个..

SharedPreferences wmbPreference;
SharedPreferences.Editor editor;
创建

wmbPreference = PreferenceManager.getDefaultSharedPreferences(this); 
boolean isActivated=wmbPreference.getBoolean("ISACTIVATED", false);

如果ISACTIVATED为false,则可以显示登录屏幕,否则跳过它。

一旦用户以这样的方式登录,您就可以编辑共享首选项。

editor = wmbPreference.edit();
editor.putBoolean("ISACTIVATED", true);
editor.commit();

答案 1 :(得分:0)

您可以这样做:

public static String KEY = "SESSION";

    // Method to store the preference :
public static void saveUserId(String mUserId, Context context) {
    Editor editor = context
            .getSharedPreferences(KEY, Activity.MODE_PRIVATE).edit();
    editor.putString("mUserId", mUserId);
    editor.commit();
}
    // Method to get the preferences :
public static String getUserId(Context context) {
    SharedPreferences savedSession = context.getSharedPreferences(KEY,
            Activity.MODE_PRIVATE);
    return savedSession.getString("mUserId", "");
}

将上述方法保留在应用的Utils类中,以保持代码的有序性。   一旦用户登录,您就可以像这样保存首选项:

  Utils.saveUserId("your id",YourActivity.this);

要检查用户是否已登录,  你可以这样做。

 If (Utils.getUserId(YourActivity.this).equals(""))
 {
   // User has not logged in. Show Registration screen here.

 }
 else
 {
 // User logged in : Skip Registration screen here.
 }

答案 2 :(得分:0)

很高兴听到您是Android App Development的新手。

我们可以在共享偏好设置的帮助下完成此操作。共享首选项是本地数据库,用户无法像sqlite数据库一样查看。创造并不困难。

执行以下步骤逐一进行操作。如果Android Manifest.xml文件中尚未提供,请添加

 <android:name="com.yourpackage.MiApplication">

添加我为您创建的更多课程。

   import android.app.Application;

   import android.content.SharedPreferences;

  import android.content.SharedPreferences.Editor;

  public class MiApplication extends Application {

public static final String PREFERENCE_NAME = "misession";

private static final String SESSION_KEY = "login_status";

private static SharedPreferences miPreferences;
private static Editor miEditor;

@Override
public void onCreate() {
    super.onCreate();
    miPreferences = getSharedPreferences(PREFERENCE_NAME, MODE_PRIVATE);
    miEditor = miPreferences.edit();
}

public static void setSession(boolean session) {
    miEditor.putBoolean(SESSION_KEY, session);
    miEditor.commit();
}

public static boolean getSession() {
    return miPreferences.getBoolean(SESSION_KEY, false);
}

}

使用setSession并在成功登录时设置为true。并使用getSession检查登录状态。如果为true则打开主页,否则打开reg。页。

注意:<android:name="com.yourpackage.MiApplication">此行非常重要。