简体   繁体   English

在Android上处理程序时感到困惑

[英]Confused on Handlers in Android

I'm trying to send a String[] to a thread in an inner class of another class(if that makes sense). 我正在尝试将String []发送到另一个类的内部类中的线程(如果这是有道理的)。 Then i want To do some work with the String[] then Output it back to the UI. 然后我想用String []做一些工作然后将它输出回UI。 But im not sure about how to do that? 但我不确定如何做到这一点? I also what to use messages so i can control what is going to be exeucted in the UI. 我还要使用消息,以便我可以控制在UI中执行的操作。

Heres my MainActivity : 我的继承人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;

    }

};

}

Heres the other class that has the in class with the Thread : 下面是具有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().
        }
    };
}

i was using handlers cuase they seemed like thats what would work for my code. 我正在使用处理程序,他们似乎对我的代码有用。 when someone clicks Line it will set a textview saying (INPUT POINT1) then the thread will wait and when the user inputs x,y,z to the edittext and click Enter the input will be put into a string then seperated by a comma and put into a string array which will be handle to the line thread then at the end of Enters code notifyAll() will be called to allow the line thread to continue and ask for the next input. 当有人点击Line它会设置一个textview说(INPUT POINT1)然后线程将等待,当用户输入x,y,z到edittext并单击Enter时,输入将被放入一个字符串,然后用逗号分隔并放入到一个字符串数组,它将处理行线程,然后在进入代码的末尾,将调用notifyAll()以允许行线程继续并请求下一个输入。 at the end of the line thread it will be handled back to the UI thread 在线程结束时,它将被处理回UI线程

Why do you want to use Handlers? 为什么要使用处理程序? I would use an AsyncTask with parameters, they are perfect in most cases, like your. 我会使用AsyncTask参数,它们在大多数情况下都很完美,就像你的。 Look at: http://developer.android.com/reference/android/os/AsyncTask.html 请查看: http//developer.android.com/reference/android/os/AsyncTask.html

I would try this ( MyAsyncTask is a subclass of your Activity class ): 我会尝试这个( 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
    }
}

From click event you call something like this: 从点击事件中你可以这样调用:

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

I want to alert you about your code: 我想提醒您有关您的代码:

@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;
    }
};

the variable Input is initialized only in the first case, if the case statement select the R.id.line or R.id.arc you are in trouble... 变量Input仅在第一种情况下初始化,如果case语句选择R.id.lineR.id.arc你遇到麻烦......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM