5

在我的应用程序中,我有三个按钮,当单击一个按钮时,它会调用一个线程来启动事情是我希望能够将 edittext 字符串输入到线程中,然后对其进行一些工作,然后将其返回到 UI 线程我可以在其中显示它或将其放入 opengl 以显示对象。我已经阅读了 Handles,但我不确定我是否完全理解它们,也许有人知道制作我自己的处理程序代码的方法。另外我已经阅读了异步,我认为它不会有利于我的应用程序。(如果它有利于我的应用程序,请告诉我)我的问题是当输入被按下时我如何从 UI 编辑文本中获取信息DrawingUtils 类中的线程然后对其完成工作然后返回到 UI 以在 openGl 程序中显示或输入?

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

        Handler UIhandler = new Handler() {
              @Override
              public void handleMessage(Message msg) {
                  Bundle bundle = msg.getData();
                    String string = bundle.getString("myKey");

                 }
             };

    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
        case R.id.enter:
             callEnter.start();
            break;
        case R.id.line:
            callLine.start();
            break;
        case R.id.arc:
            callArc.start();
            break;
        }

    };

}

这是 DrawingUtils 类:

    public class DrawingUtils {
    MainActivity handle = new MainActivity();

    // Thread classes for buttons
    public static class Enter extends Thread {
        Thread enter = new Thread() {
            public void run() {

            }

        };

        public static class Line extends Thread {
            Thread line = new Thread() {
                public void run() {

                }
            };

        }

        public static class Arc extends Thread {
            Thread arc = new Thread() {
                public void run() {

                }
            };
        }
    }
}
4

1 回答 1

6
public class MyActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstance) {
        // ....
        Handler myHandler = new Handler() {
            @Override
            public void handleMessage (Message msg) {
                doCoolStuffWhenMessageReceived();
            }
        }
        MySecondClass secondClass = new MySecondClass(myHandler);
        // ....
    }
}


public class MySecondClass {
    private handler;
    public MySecondClass(Handler handler){
        this.handler = handler;
    }

    private void someMethodToCallActivity() {
        handler.sendEmptyMessage(0);
    }

}
于 2013-06-22T04:29:41.483 回答