我有一個活動或窗體,其中有一個文本框稱爲時間在這裏。正如這個論壇的專家所建議的,我使用runnable在從wifi接收數據時更新TextBox。更新多文本框Java GUI
我的疑問是當我想更新多個TextBox的時候該怎麼做。我應該使用可運行多塊像
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;
}
}
1)請[接受](http://meta.stackexchange.com/a/65088/155831)你以前的問題的答案。 2)源代碼中的單個空白行是*總是*足夠! –
*「PLZ建議。感謝ü所有提前」 *我建議你使用正確的拼寫像「你」,「你」和「請」字。這使人們更容易理解和幫助。 –