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;

    }

};

}

继承人的另一个类具有 in 类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 时,它​​会设置一个 textview 说 (INPUT POINT1) 然后线程将等待,当用户输入 x,y,z 到 edittext 并单击 Enter 输入将被放入一个字符串中,然后用逗号分隔并放入放入一个字符串数组,该数组将被线线程处理,然后在 Enters 代码的末尾调用 notifyAll() 以允许线线程继续并请求下一个输入。在行线程结束时,它将被处理回 UI 线程

4

1 回答 1

3

为什么要使用处理程序?我会使用AsyncTaskwith 参数,它们在大多数情况下都是完美的,比如你的。看:http: //developer.android.com/reference/android/os/AsyncTask.html

我会试试这个(MyAsyncTask 是你的 Activity 类的子类):

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 回答