1

String[] を別のクラスの内部クラスのスレッドに送信しようとしています (それが理にかなっている場合)。次に、String[] を使用していくつかの作業を行い、それを UI に出力します。しかし、それを行う方法がわかりませんか?また、UI で実行される内容を制御できるように、何をメッセージに使用するかを指定します。

ここに私のMainActivity

public class MainActivity extends Activity implements OnClickListener {
EditText cl;
TextView info;
Button enter;
Button line;
Button arc;
Line callLine = new DrawingUtils.Line();
Enter callEnter = new DrawingUtils.Enter();
Arc callArc = new DrawingUtils.Arc();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    info = (TextView) findViewById(R.id.info);
    enter = (Button) findViewById(R.id.enter);
    line = (Button) findViewById(R.id.line);
    arc = (Button) findViewById(R.id.arc);
    cl = (EditText) findViewById(R.id.editText1);

    Handler uiHandler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {

            }
        }
    };

}

@Override
public void onClick(View v) {
    switch (v.getId()) {
    case R.id.enter:
        String in = cl.getText().toString();
        String[] Input = in.split(",");
        // I would like to send Input[] to the line Thread in DrawingUtils
        callEnter.start();
        break;
    case R.id.line:
        callLine.start();
        break;
    case R.id.arc:
        callArc.start();
        break;

    }

};

}

を含むクラス内を持つ他のクラスを次に示しThreadます。

public class DrawingUtils {

// Thread classes for buttons
public static class Line extends Thread {
    Thread line = new Thread() {
        public void run() {
            Looper.prepare();
            Handler lineHandler = new Handler() {
                public void handleMessage(Message msg) {
                    // How to get Input from Enter button to use in thread
                }
            };
            Looper.loop();
            // Then I need to do some work
            // Then Send the worked data back to the uiHandler in
            // oncreate().
        }
    };
}

私はハンドラーを使用していましたが、それは私のコードでうまくいくように見えました。誰かが Line をクリックすると、(INPUT POINT1) というテキストビューが設定され、スレッドは待機し、ユーザーが x、y、z を edittext に入力して Enter をクリックすると、入力が文字列に入れられ、カンマで区切られて入れられます。ラインスレッドへのハンドルとなる文字列配列に変換し、エンターの最後にコード notifyAll() が呼び出されて、ラインスレッドが続行し、次の入力を要求できるようにします。ライン スレッドの最後で、UI スレッドに戻されます。

4

1 に答える 1

3

なぜハンドラーを使用したいのですか? パラメータ付きを使用AsyncTaskしますが、ほとんどの場合、あなたのように完璧です。見てください:http://developer.android.com/reference/android/os/AsyncTask.html

私はこれを試してみます(MyAsyncTask アクティビティクラスのサブクラスです):

private class MyAsyncTask extends AsyncTask<String[], Void, Boolean> {
    //declare here local variables
    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        //prepare your local variables for the computation
    }

@Override
protected Boolean doInBackground(String[]... arg0) {

    String[] myStringArray = arg0[0];
    // make your manipulation of myStringArray

    return null; // return the result and set your local variable
}

    @Override
    protected void onPostExecute(Boolean result) {
        super.onPostExecute(result);

        //update ui using result and/or local variable
    }
}

クリックイベントから、次のように呼び出します。

String[] strings = {"1", "2", "3"};
new MyAsyncTask().execute(strings);

あなたのコードについて警告したい:

@Override
public void onClick(View v) {
    switch (v.getId()) {
    case R.id.enter:
        String in = cl.getText().toString();
        String[] Input = in.split(",");
        // I would like to send Input[] to the line Thread in DrawingUtils
        callEnter.start();
        break;
    case R.id.line:
        callLine.start();
        break;
    case R.id.arc:
        callArc.start();
        break;
    }
};

変数Inputは最初のケースでのみ初期化されます。case ステートメントが を選択した場合、R.id.lineまたはR.id.arc問題が発生した場合...

于 2013-06-23T20:08:47.357 に答える