没有单击按钮就不会传递 EditText 数据
EditText data not being passed over without button click
我正在构建一个应用程序,您可以在其中将图像上传到我公司的服务器
我的问题是我有一个包含电子邮件、密码和客户端 ID 的登录屏幕(在登录Activity中)
现在你输入的数据被传递给另一个Activity(CameraActivity),它被用来用Uri.Builder构建一个URL。
数据被保存在编辑文本框内,但它只是通过(使用意图)传递给按钮上的另一个 activity 单击因此,您必须返回登录屏幕并单击按钮重新提交数据对于每次上传和每次启动应用程序(数据存储在文本框中,但如果不单击按钮,它不会传递给另一个 activity),以便 URI.Builder 工作并提供输入信息到 URL。
我希望客户端输入一次该信息并单击一次按钮,然后每次都存储和使用它,而不管应用程序是否退出等。
登录 Activity,这个 activity 有编辑文本框,输入的信息正在保存,但没有传递给另一个 activity 没有按钮点击
public class LoginActivity extends AppCompatActivity {
String ID = "id";
String EMAIL = "email";
String PASS = "pass";
private SharedPreferences mPreference;
EditText email, password, id;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
email=findViewById(R.id.emailtext);
password=findViewById(R.id.pwdtext);
id=findViewById(R.id.clientid);
Button loginBtn=findViewById(R.id.button);
mPreference = PreferenceManager.getDefaultSharedPreferences(this);
id.setText(mPreference.getString(ID, ""));
email.setText(mPreference.getString(EMAIL, ""));
password.setText(mPreference.getString(PASS, ""));
loginBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EMAIL =email.getText().toString();
PASS =password.getText().toString();
ID =id.getText().toString();
mPreference.edit().putString(ID,ID).apply();
mPreference.edit().putString(EMAIL, EMAIL).apply();
mPreference.edit().putString(PASS, PASS).apply();
Intent intent=new Intent(LoginActivity.this,
CameraActivity.class);
intent.putExtra("clientId", ID);
intent.putExtra("email", EMAIL);
intent.putExtra("password", PASS);
startActivity(intent);
}
});
}
}
正如问题评论中所建议的,SharedPreferences 是一个不错的选择。
SharedPreferences getPreferences(Context context) {
return PreferenceManager.getDefaultSharedPreferences(context);
}
void setEmail(Context context, String email) {
SharedPreferences.Editor editor = getPreferences(context).edit();
editor.putString("EMAIL", email);
editor.apply();
}
String getEmail(Context context) {
return getPreferences(context).getString("EMAIL", "");
}
以上示例说明了如何从共享首选项中放置和检索内容。由于您要存储的不仅仅是一封电子邮件(用户 ID 等),您甚至可以将一个对象存储为 gson,因此您只需调用一个函数。但当然这是你的决定。
如果 SharedPreferences
中存在数据,您可以启动 CameraActivity
。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
email=findViewById(R.id.emailtext);
password=findViewById(R.id.pwdtext);
id=findViewById(R.id.clientid);
Button loginBtn=findViewById(R.id.button);
mPreference = PreferenceManager.getDefaultSharedPreferences(this);
//Check if data exist
if(mPreference.contain(ID))
{
Intent intent=new Intent(LoginActivity.this,
CameraActivity.class);
intent.putExtra("clientId", mPreference.getString(ID, ""));
intent.putExtra("email", mPreference.getString(EMAIL, ""));
intent.putExtra("password", mPreference.getString(PASS, ""));
startActivity(intent);
}
//Rest of the code
}
干杯:)
我正在构建一个应用程序,您可以在其中将图像上传到我公司的服务器
我的问题是我有一个包含电子邮件、密码和客户端 ID 的登录屏幕(在登录Activity中)
现在你输入的数据被传递给另一个Activity(CameraActivity),它被用来用Uri.Builder构建一个URL。 数据被保存在编辑文本框内,但它只是通过(使用意图)传递给按钮上的另一个 activity 单击因此,您必须返回登录屏幕并单击按钮重新提交数据对于每次上传和每次启动应用程序(数据存储在文本框中,但如果不单击按钮,它不会传递给另一个 activity),以便 URI.Builder 工作并提供输入信息到 URL。
我希望客户端输入一次该信息并单击一次按钮,然后每次都存储和使用它,而不管应用程序是否退出等。
登录 Activity,这个 activity 有编辑文本框,输入的信息正在保存,但没有传递给另一个 activity 没有按钮点击
public class LoginActivity extends AppCompatActivity {
String ID = "id";
String EMAIL = "email";
String PASS = "pass";
private SharedPreferences mPreference;
EditText email, password, id;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
email=findViewById(R.id.emailtext);
password=findViewById(R.id.pwdtext);
id=findViewById(R.id.clientid);
Button loginBtn=findViewById(R.id.button);
mPreference = PreferenceManager.getDefaultSharedPreferences(this);
id.setText(mPreference.getString(ID, ""));
email.setText(mPreference.getString(EMAIL, ""));
password.setText(mPreference.getString(PASS, ""));
loginBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EMAIL =email.getText().toString();
PASS =password.getText().toString();
ID =id.getText().toString();
mPreference.edit().putString(ID,ID).apply();
mPreference.edit().putString(EMAIL, EMAIL).apply();
mPreference.edit().putString(PASS, PASS).apply();
Intent intent=new Intent(LoginActivity.this,
CameraActivity.class);
intent.putExtra("clientId", ID);
intent.putExtra("email", EMAIL);
intent.putExtra("password", PASS);
startActivity(intent);
}
});
}
}
正如问题评论中所建议的,SharedPreferences 是一个不错的选择。
SharedPreferences getPreferences(Context context) {
return PreferenceManager.getDefaultSharedPreferences(context);
}
void setEmail(Context context, String email) {
SharedPreferences.Editor editor = getPreferences(context).edit();
editor.putString("EMAIL", email);
editor.apply();
}
String getEmail(Context context) {
return getPreferences(context).getString("EMAIL", "");
}
以上示例说明了如何从共享首选项中放置和检索内容。由于您要存储的不仅仅是一封电子邮件(用户 ID 等),您甚至可以将一个对象存储为 gson,因此您只需调用一个函数。但当然这是你的决定。
如果 SharedPreferences
中存在数据,您可以启动 CameraActivity
。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
email=findViewById(R.id.emailtext);
password=findViewById(R.id.pwdtext);
id=findViewById(R.id.clientid);
Button loginBtn=findViewById(R.id.button);
mPreference = PreferenceManager.getDefaultSharedPreferences(this);
//Check if data exist
if(mPreference.contain(ID))
{
Intent intent=new Intent(LoginActivity.this,
CameraActivity.class);
intent.putExtra("clientId", mPreference.getString(ID, ""));
intent.putExtra("email", mPreference.getString(EMAIL, ""));
intent.putExtra("password", mPreference.getString(PASS, ""));
startActivity(intent);
}
//Rest of the code
}
干杯:)