gpt4 book ai didi

java - 更新多个文本框 Java GUI

转载 作者:行者123 更新时间:2023-12-01 04:32:44 25 4
gpt4 key购买 nike

我有一个 Activity 或表单,其中有一个名为“时间”的文本框。正如本论坛专家的建议,我使用 runnable 来更新 TextBox,同时从 wifi 接收数据。

我的疑问是当我想更新多个文本框时该怎么办。我应该使用多个可运行 block ,例如

              time1.post(new Runnable() {
@Override
public void run() {
time2.setText(s1);
}
});

time2.post(new Runnable() {
@Override
public void run() {
time2.setText(s2);
}
});

time3.post(new Runnable() {
@Override
public void run() {
time3.setText(s2);
}
});

或者有其他技术可以更新多个文本框吗?我现在的代码如下。

package com.example.cdttiming;

public class MainActivity extends Activity
{
EditText time;
String s;
Button button;
byte[] buffer = new byte[65535];
InetAddress ia = null;
byte[] bmessage = new byte[1500];
DatagramPacket dp = new DatagramPacket(bmessage, bmessage.length);
MulticastSocket ms = null;
@Override

protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

time = (EditText) findViewById(R.id.et_time);
try
{
WifiManager wm = (WifiManager)getSystemService(Context.WIFI_SERVICE);
WifiManager.MulticastLock multicastLock = wm.createMulticastLock("multicastLock");
multicastLock.setReferenceCounted(true);
multicastLock.acquire();

ia = InetAddress.getByName("226.1.1.1");
try {
ms = new MulticastSocket(4321);
} catch (IOException e) {
e.printStackTrace();
}
try {
ms.joinGroup(ia);
} catch (IOException e) {
e.printStackTrace();
}

ms.setReuseAddress(true);

}
catch (UnknownHostException e) {
time.setText(e.getMessage());

}
catch (IOException e) {
time.setText(e.getMessage());
}
}

public void startProgress(View view) {
Runnable runnable = new Runnable() {
@Override
public void run() {
while(true) {
try {
// String str="This is test string";
ms.receive(dp);
s = new String(dp.getData(),0,dp.getLength());
char retval[] = s.toCharArray();
}
catch (UnknownHostException e) {
time.setText(e.getMessage());

}
catch (IOException e) {
time.setText(e.getMessage());
}

****////// My doubt is here if i have multple strings of data and multiple
/// multiple textboxes to update then what to do ???****

time.post(new Runnable() {
@Override
public void run() {
time.setText(s);

}
});
} // while
}
};
new Thread(runnable).start();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}

最佳答案

你的代码有点困惑。在一种情况下,您在主 while 循环中捕获数据,将其分配给字符串变量 s,然后使用文本小部件 post()函数和一个 RunnableEditText 小部件设置为该值。但在同一个 while 循环中,您可以使用异常处理程序直接设置相同的 EditText 小部件。您的代码看起来还可能会丢失消息,如果 while 循环在计时器循环有机会触发设置的文本调用之前重置 s 的值。

看来您正在尝试创建某种形式的实时系统,并且需要主要的 while 循环来持续处理并在数据可用时显示数据。现在你有 3 个不同的消费者(文本小部件),但你没有提到你是否也有 3 个不同的消息源,或者是否仍然只有一个主处理循环和某种形式的选择器将决定哪个文本小部件获取消息?

如果我沿着这些思路构建一些东西,我可能会使用消息传递系统并遵循生产者-消费者模型。当收到文本时,我会让主处理循环将一个简单的 2 字段消息推送到一个队列上,该队列包含对文本小部件的引用和对数据字符串的引用。由于字符串在 Java 中是不可变的,因此一旦消息对象拥有自己的文本副本,对 s 的任何更改都不会影响消息。

然后,我将在后台运行第二个线程来消耗消息队列。它将消息从队列中拉出,使用消息数据构建对目标文本小部件的 post 调用,将其关闭,然后返回以获取下一条消息。

通过这条路线,您可以将数据处理线程与 UI 更新处理分开,并且无需担心需要更新多少文本小部件或其他小部件。如果您需要添加其他小部件,您只需要担心创建更新消息的代码知道新小部件。执行小部件更新的线程不知道您有多少个小部件,它只是使用消息创建者所说使用的更新消息对象中引用的小部件。

关于java - 更新多个文本框 Java GUI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17790821/

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