在我的活动中我有一个主按钮,一个文本框和一个保存按钮,第一次我想要如果用户点击主按钮然后它不在另一个活动中重定向并要求在文本框中输入然后如果用户文本在文本框中输入然后在单击保存按钮后保存用户输入,再次用户只需单击主按钮,然后再启动另一个活动。
mainbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences sf = getSharedPreferences("userInput", Context.MODE_PRIVATE);
final String i = sf.getString("FirstInput", "");
if (et1.getText().toString().trim().length()>0 && et1.getText().toString().trim().equalsIgnoreCase(i)) {
Intent intent = new Intent(getApplicationContext(), WebViewOne.class);
startActivity(intent);
} else {
Toast.makeText(getApplicationContext(), "No Registered", Toast.LENGTH_LONG).show();
}
}
});
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
strInput1 = et1.getText().toString();
if (strInput1.equals("string")) {
SharedPreferences sf = getSharedPreferences("userInput", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sf.edit();
editor.putString("FirstInput", et1.getText().toString());
editor.apply();
Toast.makeText(getApplicationContext(), "Your input is Saved", Toast.LENGTH_SHORT).show();
} else
Toast.makeText(getApplicationContext(), "Your input is not matching", Toast.LENGTH_LONG).show();
}
});
答案 0 :(得分:0)
if (et1.getText.toString.trim()!=null && et1.getText().toString().trim().length()>0 && et1.getText().toString().trim().equalsIgnoreCase(i))
请同时检查空状态
答案 1 :(得分:0)
when user login successful at that time you can save login status
SharedPreferences settings = ctx.getSharedPreferences(PREFS_NAME, 0);
settings = ctx.getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("isLogin", true);
editor.commit();
after when user comes the app, at time
boolean isAlreayLogin= settings.getBoolean("isLonin",false);
if(isAlreayLogin)
//then you have to start login activity manually without any input
答案 2 :(得分:0)
该代码仅在按下按钮时有效。因此,如果您关闭应用并再次重新打开,它将不会执行任何操作,只会再次显示该表单。
我认为你需要在onCreate或onResume中实现相同的代码: