0

EclipseのコンソールとLinuxのターミナルウィンドウから動作するプログラムを作成しました。現在、Androidアプリに変換しており、作成したプログラムのJavaファイルのロジックを使用する必要がある時点までAndroidUIの基本機能を実行しています。Javaファイルからの私の入力はすべて、現在キーボードから(スキャナーから)です。

私の質問は、アプリのユーザーインタラクションで機能するようにこれを変換するにはどうすればよいですか?

唯一の入力は、組み込みのからNumberPickerです。Javaファイルはmainメソッドから始まりますpublic static void main(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コード:

public class Test {

    public static void main(String[] args) {

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

pickerValueクラスのメインメソッドに渡すにはどうすればよいですかTest(メインメソッドのロジックを開始します)?num = pickerValue;mainメソッド(を置き換える)で必要Scannerです。これはどのように行われますか?

また、私が持っている印刷ステートメントはSystem.out.println(...);、Android UIに直接変換されて画面に印刷されますか、それともそれらを変更する必要がありますか?

4

2 に答える 2

1

UI をロジックから分離するようにしてください。実際に入力を収集する部分から、入力に基づいて何かを計算している部分を抽出します。このようにして、ロジック メソッド (またはクラス) をいくつかの入力収集メソッドで再利用できます。

たとえば、Calculator クラスに次のメソッドを含めることはできません。

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

代わりに、次の 2 つのクラスに分ける必要があります。

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

このように、ハードコアな数学ロジックが Calculator クラスにカプセル化されているため、AndroidCalculator を簡単に構築できます。このクラスは、入力がどこから来るかを気にしません。

于 2012-12-02T23:02:29.037 に答える
1

Android フレームワークが理解できるようにするには、Java ロジックを書き直す必要があります。アプリの複雑さに応じて、これを行うのが簡単な場合とそうでない場合があります。

たとえば、投稿した Test クラスを使用して

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

}

あなたは正しい軌道に乗っているようです。お役に立てれば

于 2012-12-03T02:17:46.740 に答える