Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/373.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从Android活动启动Java源文件_Java_Android - Fatal编程技术网

从Android活动启动Java源文件

从Android活动启动Java源文件,java,android,Java,Android,我在Eclipse的控制台和Linux的终端窗口中编写了一个程序。我现在正在将它转换成一个Android应用程序,我已经完成了Android UI的基本功能,直到它需要使用我编写的程序的Java文件中的逻辑为止。Java文件中的所有输入当前都来自键盘(来自扫描仪) 我的问题是:我如何转换它,使其与应用程序的用户交互工作 唯一的输入将来自内置的NumberPicker。Java文件从main方法开始:publicstaticvoidmain(String[]args){) 例如: Android代

我在Eclipse的控制台和Linux的终端窗口中编写了一个程序。我现在正在将它转换成一个Android应用程序,我已经完成了Android UI的基本功能,直到它需要使用我编写的程序的Java文件中的逻辑为止。Java文件中的所有输入当前都来自键盘(来自扫描仪)

我的问题是:我如何转换它,使其与应用程序的用户交互工作

唯一的输入将来自内置的
NumberPicker
。Java文件从main方法开始:
publicstaticvoidmain(String[]args){

例如:

Android代码示例:

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);
}
来自程序(Test.Java)的Java代码示例:

如何将
pickerValue
传递到
Test
类的main方法中(启动main方法的逻辑)?我希望在main方法中使用
num=pickerValue;
(替换
扫描器
),这是如何做到的


还有,我的print语句,
System.out.println(…);
,直接翻译到Android UI并在屏幕上打印,还是我必须修改它们?

您应该尝试将UI与逻辑分离。从实际收集输入的部分提取基于输入计算的部分。这样,逻辑方法(或类)可以与多种输入收集方法一起重用

例如,计算器类不应具有以下方法:

/**
 * 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() {...}
相反,应将其分为两类:

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() {...}
}

这样,构建AndroidCalculator就很容易了,因为核心的数学逻辑封装在一个Calculator类中,它不关心输入来自何处。

您必须重新编写Java逻辑才能被Android框架理解。根据应用程序的复杂程度,这可能很容易做到,也可能不容易做到

例如,使用您发布的测试类

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();
...
}
}
这很容易变成

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

}

你似乎在正确的轨道上。希望这能有所帮助

对不起,但我看不出这能回答我的问题。我正在寻找如何启动我的逻辑代码,并将用户输入从Android传输到它。重新阅读我的答案。不要用一个主方法来完成所有工作,而是将计算部分提取到它自己的方法,甚至它自己的类中.从你的Android代码调用方法。如果你重新阅读我的问题,这就是我要问的问题。我已经做好了一切准备。我要问的是如何对main方法进行特定调用,同时将一个整数值从Android代码传递到main。我将Android代码和Java分开,现在如何从Android启动Java?以及我的观点是:你的Android代码不应该调用main()。它应该调用一个单独的方法,该方法不处理输入和输出,只处理计算。将整个程序放入主方法中是不可接受的。将主方法拆分为方法和类。看看javadoc:它充满了可重用的类和方法。它不是由单个main()组成的方法。对您的代码也要这样做:提取输入和输出的方法,以及其他计算方法。我真的很困惑。主要方法是什么启动了Java程序?主要方法是从哪里调用我的所有方法;它是核心。当我只有一堆r抛出的andom方法?我的main方法有一个整数变量。从main调用一个方法来对该数字进行计算。完成后,它在main方法中继续运行。调用另一个方法来进行另一次计算,等等。因此在您的解释中,程序背后的驱动力在哪里?核心是什么?Java我的问题中的代码来自一个完全独立的文件/类,而不是Android代码。我在Android代码中计算出了我程序的所有逻辑。我基本上是试图从Android活动中运行该逻辑代码。该活动将有一个
NumberPicker
,我想用它替换扫描仪。该活动将有一个提示,提示用户输入一个数字。用户用
NumberPicker
输入数字,点击“OK”,然后Java逻辑代码将进行计算。它将继续这样做,直到逻辑代码中的循环中断。我基本上在寻找类似的解决方案:我传递一个整数(1个整数)在我的示例Java代码中,我希望将整数设置为
num
。关于如何从Android启动我的程序,您有什么进一步的意见吗?
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);

}