简体   繁体   中英

Starting Java Source File from Android Activity

I wrote a program to work from the console in Eclipse and the terminal window in Linux. I am now transforming it into an Android app and I have the basic functionality of the Android UI done up until the point where it needs to use the logic from the Java file of the program I wrote. All of my inputs from the Java file are currently from the keyboard (from Scanners).

My question is: how do I transform this to get it work with the user interaction of the app?

The only input would be from the built in NumberPicker . The Java file starts from the main method: public static void main(String[] args) { )

For example:

Sample Android Code:

public class Source1 extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.source1_layout);
        Intent intent = getIntent();
        int pickerValue = intent.getIntExtra("numEntered", 0);
}

Sample Java Code from program (Test.java):

public class Test {

    public static void main(String[] args) {

    System.out.println("Enter number: ");
    Scanner reader = new Scanner(System.in);
    int num = reader.nextInt();
    ...
    }
}

How do I pass pickerValue into the main method of the Test class (starting the logic of the main method)? I want num = pickerValue; in the main method (replacing the Scanner ). How is this done?

Also, will the print statements I have, System.out.println(...); , translate directly into the Android UI and print on the screen or do I have to modify those?

You should try to separate the UI from the logic. Extract the part where you're computing something based on inputs from the part where you actually gather the input. That way, the logic methods (or classes) can be reused with several input-gathering methods.

For example, a Calculator class should not have the following methods:

/**
 * asks for two numbers A and B, reads them from the keyboard, and displays the result 
 * of A^B on the screen
 */
public void power() {...}

/**
 * asks for a number A, reads it from the keyboard, and displays the square root
 * of A on the screen
 */
public void squareRoot() {...}

Instead, it should be separated into two classes:

public class Calculator {
    /**
     * returns a^b
     */
    public double power(double a, double b) {...}

    /**
     * returns the square root of a
     */
    public double squareRoot(double a) {...}
}

public class ConsoleCalculator {
    private Calculator calculator;

    public ConsoleCalculator(Calculator calculator) {
        this.calculator = calculator;
    }

    /**
     * asks for two numbers A and B, reads them from the keyboard, uses
     * the calculator to compute A^B, and displays the result on the screen
     */
    public void power() {...}

    /**
     * asks for a number A, reads it from the keyboard, uses the calculator to
     * compute the square root of A and displays the result on the screen
     */
    public void squareRoot() {...}
}

This way, it's easy to build an AndroidCalculator, because the hard-core math logic is encapsulated in a Calculator class, which doesn't care about where the inputs come from.

You'll have to re-write your Java logic for it to be understood by the Android framework. Depending on the complexity of your app, this may or may not be easy to do.

For example, using the Test class you posted

Sample Java Code from program:

public class Test {

public static void main(String[] args) {

System.out.println("Enter number: ");
Scanner reader = new Scanner(System.in);
int num = reader.nextInt();
...
}
}

This would easily become

public class Source1 extends Activity {

TextView number;
 EditText enterText;
 String result;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.source1_layout);

//inflate your components from XML
result = (TextView)findViewById(R.id.resultId);
enterText = (EditText)findViewById(R.id.enterTextId);

//Get the value from the user
//print it to the screen
String userWrites = enterText.getText().toString();
result.setText(userWrites);

}

You seem to be on the right track. Hope this helps

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