简体   繁体   中英

Share Method between two activities (android)

I want to share a static method called register between two activities. register gets called when a button is clicked.

Basically, I have one activity for retail customer registration and another one for business customers (additional info needed for business customers).

My question is : What do I pass in the parameter of register so I may have access to the controls of the Activity that called register .

For example, if the customer registration form activity called register then :

EditText fn = (EditText) findViewById(R.id.firstName);
EditText ln = (EditText) findViewById(R.id.lastName);
EditText ad = (EditText) findViewById(R.id.address);
EditText c = (EditText) findViewById(R.id.city);

would return controls on the retail customer activity form else it would return business customer activity form controls. I gave the controls the same name.

And once again, Thanks :D

EDIT:

Class where register would go:

import android.view.View;
import android.widget.EditText;


    public class Helper {

        public static void register(\\parameters)
        {

               EditText fn = (EditText) findViewById(R.id.firstName);
               EditText ln = (EditText) findViewById(R.id.lastName);
               EditText ad = (EditText) findViewById(R.id.address);
               EditText c = (EditText) findViewById(R.id.city);
               ...        

        }




    }

Edit: I made a Customer Class and a Commercial Customer Class that extends Customer class and when on click register (for commercial customers) is called I create a new commercial customer and register them. I did the same thing for regular Customers.

CommercialCustomer cust = new CommercialCustomer(businessN.getText().toString(),fn.getText().toString(),ln.getText().toString(),
            bFn.getText().toString(),bLn.getText().toString(),ad.getText().toString(),
            c.getText().toString(),s.getText().toString(),zip.getText().toString()
            ,pN.getText().toString(),em.getText().toString(),uN.getText().toString(),pw.getText().toString());
    cust.createCustomerAccount();

Something like this should work.

MyActivity:
...

           EditText fn = (EditText) findViewById(R.id.firstName);
           EditText ln = (EditText) findViewById(R.id.lastName);
           EditText ad = (EditText) findViewById(R.id.address);
           EditText c = (EditText) findViewById(R.id.city);
           //pass as parameter into constructor or method.
           Helper helper = new Helper(fn, ln, ad, c);
           helper.myRegisterMethod();

           //or
           helper.myRegisterMethod(fn, ln, ad, c);
           ...        


public class Helper {

//class constructor
    public Helper (fn, ln, ad, c)
    {

        this.fn = fn;
        etc
    }

    myRegisterMethod(){
    //or
    myRegisterMethod(fn, ln, ad, c){
          //to do       

    }
}

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