There was a problem with the Android studio using sharedpreference to practice login. I ask for your help me.

Asked 1 years ago, Updated 1 years ago, 94 views

Membership registration activity Login Activity Login Result Activity As a result of log-in, we'll divide it into three Check the login value and if the membership information is correct, move on to the next activity If the login value doesn't exist, it's making it impossible to press the button The login results are printed correctly, but the button is always pressed. Please give me some advice.

// Membership registration activity 
ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText (RegisterActivity.this, "Member registration completed", Toast.LENGTH_SHORT).show();

                SharedPreferences preferences = getSharedPreferences("MYPREFS", Context.MODE_WORLD_READABLE);

                String newUser = user.getText().toString();
                String newPassword = pass.getText().toString();
                String newEmail = email.getText().toString();

                SharedPreferences.Editor editor = preferences.edit();
                editor.putString("newEmail", newEmail);
                editor.commit();
                editor.putString("newPassword", newPassword);
                editor.commit();
                editor.putString (newEmail + newPassword + "data", newUser + " Welcome" + "\n" + newEmail);
                editor.commit();

                Intent intent = new Intent(RegisterActivity.this, MainActivity.class);

                startActivity(intent);
            }
        });

//Login Activity
login.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {
                String login_email = email.getText().toString();
                String password = pass.getText().toString();
                SharedPreferences preferences = getSharedPreferences("MYPREFS", MODE_PRIVATE);

                StringuserDetails = preferences.getString (login_email + password + "data", "Member that does not exist";
                SharedPreferences.Editor editor = preferences.edit();
                editor.putString("display", userDetails);
                editor.commit();

                Intent displayScreen = new Intent(MainActivity.this, DisplayScreen.class);
                displayScreen.putExtra("elogin",login_email);
                displayScreen.putExtra("epass",password);
                startActivity(displayScreen);
            }
        });

//Login result activity
final Button ok = (Button) findViewById(R.id.button_ok);

        SharedPreferences preferences = getSharedPreferences("MYPREFS", Context.MODE_WORLD_READABLE);
        String id = preferences.getString("newEmail","");
        String ps = preferences.getString("newPassword","");
        String display = preferences.getString("display", "1");

        TextView displayInfo = (TextView) findViewById(R.id.textViewName);
        displayInfo.setText(display);

        if (display.equals("1")) {
            ok.setClickable(false);

        } } else  {
            ok.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(DisplayScreen.this, AppActivity.class);
                    startActivity(intent);
                }
            });
        }

sharedpreferences

2022-09-22 18:17

1 Answers

I can't tell when all the codes you uploaded will be called First of all, if (display.equals("1") I think you should check if the display is coming out properly with 1. if (display.If equals("1") is true and clicks even though you rode the setClickable(false) syntax, check to see if you have registered setOnClickListener elsewhere.

For your information, if setOnClickListener is setClickable(false); before attaching a click listener, clickable becomes true again.

If you look at the code inside the Android, it looks like the one below.

public void setOnClickListener(@Nullable OnClickListener l) {
        if (!isClickable()) {
            setClickable(true);
        }
        getListenerInfo().mOnClickListener = l;
    }

Therefore, we recommend setEnabled(false) more than setClickable(false) to prevent clicks. To make sure it's stopped.


2022-09-22 18:17

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.