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 スレッドに戻されます。