简体   繁体   中英

Calling functions from other Java files in the same project

I have an app that has 4 activities so far. All of these activities have text input from the user (using edit text). All of these user input sections will require the same exact type of 'error checking' to ensure the user enters variables of the correct data type (in this case, numbers).

Is it possible (I'm assuming anything is possible) to make another java file in the 'SRC' folder where I could write and store this function to then call in every instance where I would need it? This java file would not have a layout associated with it; it would only be used for this function (and probably others as I work on the app)

So far I haven't been able to find a good example of this to reverse engineer, and it seems silly to me to have to write out the same piece of code 4+ times.

I didn't post examples of my code, as it seems pointless since I think I just need to how what I would import into each activity.

You are right. Create another Java file lets say Utility.java , have all those functions as static . Then call those functions as Utility.functionName(String params) in other java files. Eg:

public class Utility{

    public static boolean functionName(){
        //....
        return true;  
    }
    // and so on...

}

In other files import the class Utility and access the functions like:

boolean var = Utility.functionName();

Yes. You can create a java file and keep the common functions there. I do something like this. I normally name the file as Utils.java:

public class Utils {

    public static String downloadFileFromInternet(String url)
    {
        if(url == null || url.isEmpty() == true)
            new IllegalArgumentException("url is empty/null");
        StringBuilder sb = new StringBuilder();
        InputStream inStream = null;
        try
        {
            url = urlEncode(url);
            URL link = new URL(url);
            inStream = link.openStream();
            int i;
            int total = 0;
            byte[] buffer = new byte[8 * 1024];
            while((i=inStream.read(buffer)) != -1)
            {
                if(total >= (1024 * 1024))
                {
                    return "";
                }
                total += i;
                sb.append(new String(buffer,0,i));
            }
        }catch(Exception e )
        {
            e.printStackTrace();
            return null;
        }catch(OutOfMemoryError e)
        {
            e.printStackTrace();
            return null;
        }
        return sb.toString();
    }

    public static String urlEncode(String url)
    {
        if(url == null || url.isEmpty() == true)
            return null;
        url = url.replace("[","");
        url = url.replace("]","");
        url = url.replaceAll(" ","%20");
        return url;
    }
}

Now in any java file you can use it with the Class name.

Hope this helps.

You can define your own class, with your own method and import it normally, via the import statement at the top. Using static methods will work just fine.

You'll need to specify the package where you've put your class to use the import statement.

Yes you can do this, just make a class A in your java file, that class contain the method you want (foo()), and then, in your activity, instantiate your class A, then make a call to the method:

public class MyActivity extends Activity {
   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       A a = new A();
       a.foo();
   }
}

class A {
    public void foo() {
        // method implementation
    }
}

You can also use the static way :

public class MyActivity extends Activity {
   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);

       A.foo();
   }
}

class A {
   public static void foo() {
       // method implementation
   }
}

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