使用sharedPreference的Android登录/注册 [英] Android Login/Registration using sharedPreference

查看:135
本文介绍了使用sharedPreference的Android登录/注册的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用sharedpreference进行登录/注册活动.该应用程序将仅保留一个登录名/密码信息.

I'm trying to make a Login/Registration activity by using sharedpreference. The app will only hold one login/password information.

因此,在原始启动时,应用程序将检查用户是否已经建立了帐户.这将保留在名为Registered的布尔值下.如果是这样,它将启用登录按钮并禁用注册按钮.如果为假,则将启用注册按钮并禁用登录按钮.

So on original start up the app will check if the user has already made an account. This will be kept under a boolean called Registered. If that is true then it will enable the login button and disable the register button. If it is false then it will enable the register button and disable the login button.

当输入所需的用户名和密码时,他们可以单击注册按钮,这会将用户名和密码设置为字符串,同时将布尔值更改为true,然后将其放入sharedpreferences中.然后,活动将刷新.

When the types in his wanted username and password they can then hit the register button which will set the username and password into strings while changing the boolean to true and then put them in the sharedpreferences. Then the activity will refresh itself.

目前,我无法更改按钮.我认为我可能在执行sharedpreferences时出错,因为这是我第一次与他们合作. (我是android开发的新手.)

Currently i cannot get the buttons to change. I think i may be doing sharedpreferences wrong as this is my first time working with them. (Im new to android development.)

这是我当前的代码.

我已更新代码以包含editor.apply()

I have updated the code to include editor.apply()

我添加了一些点滴,主要是在开始时获取值.

I have added a few bits and pieces, mainly getting the value at start.

最终更新:我已经解决了这个问题.它似乎在我用于按钮的.setenabled内.一旦我将它们切换到.setvisibility,它们就会完美切换.谢谢大家的帮助!

FINAL UPDATE: I have fixed the problem. It seemed to lie within the .setenabled i was using for the buttons. Once i switched them to .setvisibility, they switched perfectly. Thank you all for your help!

public class MainActivity extends AppCompatActivity {


EditText Username_input, Password_input;
String Username, Password;

//if false then no, if true then yes
boolean Registered;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
    Registered = sharedPref.getBoolean("Registered", false);

    Button RegisterButton = (Button) findViewById(R.id.Register_btn);
    Button Loginbutton = (Button) findViewById(R.id.Login_btn);

    // If the user is registered.
    if (Registered == false) {

        Loginbutton.setEnabled(false);
        RegisterButton.setEnabled(true);

        // If the user is registered already.
    } else {

        Loginbutton.setEnabled(true);
        RegisterButton.setEnabled(false);
    }

    RegisterButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Username_input = (EditText) findViewById(R.id.Username_input);
            Password_input = (EditText) findViewById(R.id.Password_input);
            Username = Username_input.getText().toString();
            Password = Password_input.getText().toString();


            SharedPreferences.Editor editor = sharedPref.edit();
            editor.putBoolean("Registered", true);
            editor.putString("Username", Username);
            editor.putString("Password", Password);
            editor.apply();

            finish();
            startActivity(getIntent());

        }
    });
}
}

XML代码

XML Code

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="@string/Username_txtview"
    android:id="@+id/Username_txtview"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginLeft="32dp"
    android:layout_marginStart="32dp"
    android:layout_marginTop="32dp"
    android:editable="false" />

<EditText
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/Username_input"
    android:enabled="true"
    android:layout_marginLeft="32dp"
    android:layout_marginRight="32dp"
    android:layout_below="@+id/Username_txtview"
    android:layout_centerHorizontal="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="@string/Password_txtview"
    android:id="@+id/Password_txtview"
    android:layout_below="@+id/Username_input"
    android:layout_alignLeft="@+id/Username_input"
    android:layout_alignStart="@+id/Username_input" />

<EditText
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/Password_input"
    android:enabled="true"
    android:layout_marginLeft="32dp"
    android:layout_marginRight="32dp"
    android:layout_below="@+id/Password_txtview"
    android:layout_centerHorizontal="true" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="80dp"
    android:layout_marginRight="80dp"
    android:text="@string/Login_btn"
    android:id="@+id/Login_btn"
    android:layout_centerVertical="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/Register_btn"
    android:id="@+id/Register_btn"
    android:layout_below="@+id/Login_btn"
    android:layout_marginLeft="80dp"
    android:layout_marginRight="80dp"
    android:layout_marginTop="26dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
     />

</RelativeLayout>  

推荐答案

final SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
Registered = sharedPref.getBoolean("Registered", false);

if (!Registered) {

    Loginbutton.setVisibility(View.GONE);
    RegisterButton.setVisibility(View.VISIBLE);

    // If the user is registered already.
} else{

    Loginbutton.setVisibility(View.VISIBLE);
    RegisterButton.setVisibility(View.GONE);
}

RegisterButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Username_input = (EditText) findViewById(R.id.Username_input);
        Password_input = (EditText) findViewById(R.id.Password_input);
        Username = Username_input.getText().toString();
        Password = Password_input.getText().toString();

        SharedPreferences.Editor editor = sharedPref.edit();
        editor.putBoolean("Registered", true);
        editor.putString("Username", Username);
        editor.putString("Password", Password);
        editor.apply();

        finish();
        startActivity(getIntent());

    }
});

这篇关于使用sharedPreference的Android登录/注册的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆