第一次启动时将 Activity 设置为默认 Activity

我编写了一个 Android 应用程序,用于检查 EditText 中输入的字符串是否与共享首选项中存储的字符串相同,如果错误,则会出现一个 Toast,告诉用户他刚刚输入的字符串不正确,这是真的。活动已开放。我希望每次用户打开应用程序时都打开此打开的活动。


这是我尝试的:


private PreferenceHelper preferenceHelper;

private ParseContent parseContent;

private final int RegTask = 1;

private EditText editText;

private Button button;

private String string;


@Override

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_login2);


    parseContent = new ParseContent(this);

    preferenceHelper = new PreferenceHelper(this);


    editText = findViewById(R.id.code);

    button = findViewById(R.id.abonnement);


    final SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0);


    button.setOnClickListener(new View.OnClickListener() {


        @Override

        public void onClick(View v) {

            string = editText.getText().toString();

            if (string.equals(pref.getString("code", null))){

                Intent intent = new Intent(Login.this, WelcomeActivity.class);

                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);

                startActivity(intent);

            }else {

                Toast.makeText(Login.this, "Désolé mais votre code est incorrect",Toast.LENGTH_LONG).show();

            }

        }

    });

}

当用户输入正确的代码(即共享首选项中先前注册的代码)时,会WelcomeActivty.class打开,但当用户退出应用程序并再次进入时,Login.class始终会显示,而我希望WelcomeActivity.class每次用户再次进入应用程序时都会启动。 ..请问我该怎么做?


ibeautiful
浏览 106回答 4
4回答

繁星coding

注意:我制作了一个登录系统(没有密码),即使您没有在登录系统上工作,我仍然认为该技术仍然适用于您的情况。尝试添加另一个存储布尔值的共享首选项,以检查用户是否在线。例如,在您的 LoginActivity 中,添加editor.putBoolean("isOnline", true); 编辑器.apply();当您单击登录按钮时。同样,当您注销时,只需输入editor.putBoolean("isOnline", false); 编辑器.apply();我是按照你的问题做的。MainActivity.java(这是登录活动)  public class MainActivity extends AppCompatActivity {    SharedPreferences pref;    SharedPreferences.Editor editor;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        final EditText et = findViewById(R.id.et);        Button btnRegister = findViewById(R.id.btn_register);        Button btnLogin = findViewById(R.id.btn_login);        pref = getSharedPreferences("MyPref", MODE_PRIVATE);        editor = pref.edit();        btnRegister.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                editor.putString("username", et.getText().toString());                editor.putBoolean("isOnline", false);                editor.apply();            }        });        btnLogin.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                if (et.getText().toString().equals(pref.getString("username", null))) {                    Intent intent = new Intent(getApplicationContext(), WelcomeActivity.class);                    intent.putExtra("name", et.getText().toString());                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);                    editor.putBoolean("isOnline", true);                    editor.apply();                    startActivity(intent);                    finish();                }                else {                    Toast.makeText(MainActivity.this, "Incorrect input", Toast.LENGTH_LONG).show();                }            }        });// This is when user has not clicked the log out button, then we go to the WelcomeActivity instead        if (pref.getBoolean("isOnline", false)) {            startActivity(new Intent(getApplicationContext(), WelcomeActivity.class));            finish();        }    }}WelcomeActivity.java(登录后访问的Activity)public class WelcomeActivity extends AppCompatActivity {    SharedPreferences pref;    SharedPreferences.Editor editor;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_welcome);        pref = getSharedPreferences("MyPref", MODE_PRIVATE);        editor = pref.edit();        editor.putBoolean("isOnline", true);        editor.apply();        Button btnLogout = findViewById(R.id.logout);        btnLogout.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                // When user clicks on the logout button, we set this to false. So everything will be back to normal.                editor.putBoolean("isOnline", false);                editor.apply();                if (Build.VERSION.SDK_INT > Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {                    Intent intent = new Intent(getApplicationContext(), MainActivity.class);                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);                    startActivity(intent);                    finishAffinity();                }                else {                    Intent intent = new Intent(getApplicationContext(), MainActivity.class);                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);                    startActivity(intent);                    finish();                }            }        });    }}

MMTTMM

将输入的值保存在共享首选项中,并使用“EncryptedSharedPreferences”以确保安全在 onCreate 检查用户是否输入了值并在进行检查之前保存if (string.equals(pref.getString("code", null))){        Intent intent = new Intent(Login.this, WelcomeActivity.class);      intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);      startActivity(intent);`enter code here`}并完成当前活动所以现在下一个屏幕将自动打开

梵蒂冈之花

您需要更改您的逻辑,当您打开应用程序时,Login.class 将是您的第一个屏幕,在该屏幕中您必须检查输入的代码是否与共享首选项值类似,如果是 true,则将登录屏幕移至欢迎屏幕,希望我的回答对您有所帮助

梦里花落0921

伙计,这可以通过创建会话来完成。就像当您的编辑文本与所需文本匹配时,您应该在共享首选项中分配一个值,该值将在创建会话时充当。button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { string = editText.getText().toString(); if (string.equals(pref.getString("code", null))){    SharedPreferences.Editor edit = pref.edit();    edit.putString("Session", "1");    edit.apply();    Intent intent = new Intent(Login.this, WelcomeActivity.class);  intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);  startActivity(intent);}else {  Toast.makeText(Login.this, "Désolé mais votre code est incorrect",Toast.LENGTH_LONG).show();}现在在您所需屏幕的 onCreate 方法中。只需检查会话值是否包含在您的 SharedPreferences 中,然后根据您的需要执行任何操作。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java