gpt4 book ai didi

java - 如何在另一个类中实现处理程序?

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:32:37 24 4
gpt4 key购买 nike

在我的应用程序中,我有三个按钮,当单击一个按钮时,它会调用一个线程来启动事情是我希望能够将 edittext 字符串输入到线程中,然后对其进行一些处理,然后将其返回到我可以在其中显示它或将其放入 opengl 以显示对象的 UI 线程。我已经阅读了有关 Handles 的内容,但我不确定我是否完全理解它们,也许是否有人知道制作我自己的处理程序代码的方法。我也读过异步,我不认为它会有益于我的应用程序。(个人意见如果它有益于我的应用程序让我知道)我的问题是当输入被按下到行时我如何从 UI edittext 获取信息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() {

}
};
}
}
}

最佳答案

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

}

关于java - 如何在另一个类中实现处理程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17247271/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com