简体   繁体   中英

How can i pass editTexts from Activity class to nonActivity

My Activity Login has edittext's value first entered by username and second value by password. I'm trying to pass this values of editText from Activity Class to nonActivityClass(Java Class).

Does exist any way to do this?

I do not know why you want to do that. But you can do this using Preferences. At your Activity save your data to Pref and load them at your Class

It depends on what you want to do with the data, I'm not sure what your issue is. You can use a Class with a Static method which can accept username and passwords then do your checking there or what ever operation you want after which you can return the result. There are other options to try out. Just like database classes which are not 'Activity classes', you can also pass values to them.

As in any normal class in Java with constructors:

User newuser = new User(uname, pwd); // in your activity
// in your class
public User(String uname,String pwd){
      this.uname = uname;
      this.pwd = pwd;
      }

Please also consider to learn java basics before use it in android.

yourButtonLogin.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
        String username = yourEditTextUsername.getText().toString();
        String password = yourEditTextPassword.getText().toString();
        Intent intent = new Intent(this,nonActivityClass.class);
        intent.putExtra("username",username);
        intent.putExtra("password",password);
        startActivity(intent);
    }
});

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.YOUR_LAY_OUT);
    Intent intent = getIntent();
    if(intent != null){
      String username = intent.getStringExtra("username");
      String password = intent.getStringExtra("password");
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM