简体   繁体   English

如何在Android中为多个线程使用多个按钮?

[英]How to use multiple buttons for multiple threads in android?

I've been working on this program for some time now and. 我现在已经从事此程序一段时间了。 I was stuck on how to handle my multiple buttons issue.I have three buttons that needs to start different threads but i've looked at the the stuff on google for threading and multithreading and i couldnt find the answer i was looking for. 我被困在如何处理我的多个按钮问题上。我有三个按钮需要启动不同的线程,但是我已经查看了Google上用于线程化和多线程的东西,但我找不到我想要的答案。 From my understanding public void run() can only be called once in an class for threads? 据我了解,public void run()只能在线程类中被调用一次? How would i create multiple threads that differ in code in one class? 如何在一个类中创建代码不同的多个线程?

Example of what i have seen that would be the best solution to my problem is: 我所见为最佳解决方案的示例是:

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()) {
    case R.id.Line:
       // Call to Thread line
        break;
    case R.id.Enter:
        //Call to Thread Enter
            break;
    case R.id.arc
    //Call to Thread Arc
        } 

Example of the line thread and enter thread: 行线程和输入线程的示例:

        Thread enter = new Thread() {
        public void run() {
    DrawingUtils call = new DrawingUtils();
    EditText cl = (EditText) findViewById(R.id.editText1);
    String in = cl.getText().toString();
    call.setInputCoords(in);
    notifyAll();
        }

};

        Thread line = new Thread() {
        public void run() {
            info.setText("Enter X,Y,Z for Point 1");
            try {
                wait();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            call.addLine();
            info.setText("Enter X,Y,Z for Point 2");
            try {
                wait();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            call.addLine();


        }
    };
    line.start();

Create other inner class which extends thread like 创建其他内部类来扩展线程,例如

    class Line extends Thread {
        public void run() {
    DrawingUtils call = new DrawingUtils();
    EditText cl = (EditText) findViewById(R.id.editText1);
    String in = cl.getText().toString();
    call.setInputCoords(in);
    notifyAll();
        }

};

now start using new Line().start() 现在开始使用new Line().start()

U Cant update UI normally on new thread which you launch.. please read more about UI before development..... this may help you U Cant通常会在您启动的新线程上更新UI。请在开发之前阅读有关UI的更多信息…… 这可能对您有帮助

to set test for text view in your code 在代码中为文本视图设置测试

info.post(new Runnable() {
 @Override
    public void run() {
       info.setText("Enter X,Y,Z for Point 1"); 
    }
});

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

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