【问题标题】:EditText data not being passed over without button clickEditText 数据不会在没有按钮单击的情况下被传递
【发布时间】:2019-08-14 22:02:36
【问题描述】:

我正在构建一个应用程序,您可以在其中将图像上传到我的公司服务器

我的问题是我有一个包含电子邮件、密码和客户 ID 的登录屏幕(在 LoginActivity 中)

现在,您在此处输入的数据将传递给另一个 Activity(CameraActivity),用于使用 Uri.Builder 构建 URL。 数据被保存在编辑文本框中,但它只是被传递(使用意图)到按钮单击上的其他活动,因此,您必须返回登录屏幕并单击按钮以重新提交每次上传的数据和每次启动应用程序时(数据存储在文本框中,但没有单击按钮就不会传递给其他活动),URI.Builder 工作并将输入的信息提供给 URL。

我希望客户一次输入该信息并单击一次按钮,然后每次都存储和使用它,而不管应用程序是否退出等等。

登录活动,此活动具有编辑文本框,输入的信息正在保存,但不通过按钮单击就不会传递给其他活动

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

【问题讨论】:

  • 使用 sharedpreference 来存储数据。并使用 startActivityForResult() 并实现 onActivityResult() 并处理所有事情,以便在返回登录屏幕后用户不需要单击重新提交按钮。
  • 所以我必须用 sharedprefrence 替换 mprefrence 还是什么?我只做了大约 2 周的编码,所以任何帮助都将不胜感激
  • 是的,继续。在 CameraActivity 中使用 sharedpreference 来检索保存的数据。
  • 对不起,我真的不知道该怎么做

标签: java android android-edittext


【解决方案1】:

正如问题评论中所建议的,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,因此您只需调用一个函数。但当然这是你的电话。

【讨论】:

    【解决方案2】:

    如果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
    }
    

    干杯:)

    【讨论】:

      猜你喜欢
      • 2011-10-17
      • 2020-07-28
      • 1970-01-01
      • 2021-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-20
      相关资源
      最近更新 更多