gpt4 book ai didi

java - 首次启动时,将 Activity 设置为默认 Activity

转载 作者:行者123 更新时间:2023-11-29 08:21:14 26 4
gpt4 key购买 nike

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

这是我的尝试:

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....请问我该怎么做?!

最佳答案

注意:我做了一个登录系统(没有密码),即使你没有在登录系统上工作,我仍然认为这个技术仍然适用于你的情况。

尝试添加另一个存储 boolean 值的 sharedpreference 以检查用户是否在线。例如,在您的 LoginActivity 中,添加

editor.putBoolean("isOnline", true); editor.apply();

当您点击登录按钮时。

同样,当你登出时,只要把

editor.putBoolean("isOnline", false); editor.apply();

根据你的问题,我就是这样做的。

MainActivity.java(这是登录 Activity )

  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();
}
}
});
}
}

关于java - 首次启动时,将 Activity 设置为默认 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57932905/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com