Rumah > Soal Jawab > teks badan
1.但是一直得不到数据……
这个是接受数据线程
private class ConnectedThread extends Thread {
private final BluetoothSocket socket;
private final InputStream inputStream;
private final OutputStream outputStream;
public ConnectedThread(BluetoothSocket socket) {
this.socket = socket;
InputStream input = null;
OutputStream output = null;
try {
input = socket.getInputStream();
output = socket.getOutputStream();
} catch (IOException e) {
e.printStackTrace();
}
this.inputStream = input;
this.outputStream = output;
}
public void run() {
byte[] buff = new byte[20];
int bytes;
while (true) {
try {
bytes = inputStream.read(buff);
String str = new String(buff, "ISO-8859-1");
str = str.substring(0, bytes);
Log.e("recv", str);
System.out.println("**log:"+str);
Message message=handler_accept.obtainMessage();
message.obj=str;
handler_accept.sendMessage(message);
} catch (IOException e) {
e.printStackTrace();
break;
}
}
}
public void write(byte[] bytes) {
try {
outputStream.write(bytes);
} catch (IOException e) {
e.printStackTrace();
}
}
public void cancel() {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
---这个是handler
Handler handler_accept=new Handler(){
@Override
public void handleMessage(Message msg) {
String s= (String) msg.obj;
timeStr=timeStr+s;
//数据
}
}
}
};
popupwindows,这个显示调用方法是在按钮的点击事件中
private void showPopupWindow(View view) {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 一个自定义的布局,作为显示的内容
View contentView = LayoutInflater.from(Zhuye_Activity.this).inflate(R.layout.layout_timeupdate, null);
// 设置按钮的点击事件
final PopupWindow popupWindow = new PopupWindow(contentView,
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, true);
WindowManager manager=(WindowManager) getSystemService(Context.WINDOW_SERVICE);
popupWindow.setTouchable(true);
int xpos=manager.getDefaultDisplay().getWidth()/2-popupWindow.getWidth()/2;
//xoff,yoff基于anchor的左下角进行偏移。
TextView textView1 = (TextView) contentView.findViewById(R.id.textView_xiugai);
TextView textView2 = (TextView) contentView.findViewById(R.id.textView_quxiao);
final TextView textView_shebeitime= (TextView) contentView.findViewById(R.id.textView_shebeitime);
textView_shebeitime.setText(textView1_neiyong.getText().toString());
TextView textView_nowtime= (TextView) contentView.findViewById(R.id.textView_nowtime);
SimpleDateFormat sDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String date=sDateFormat.format(new java.util.Date());
textView_nowtime.setText(date);
textView2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
popupWindow.dismiss();
}
});
textView1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) { screenshot();
String s=Utils.Timeform();
try {
outStream.write(s.getBytes());
outStream.write("\r\n".getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
});
popupWindow.setTouchInterceptor(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return false;
// 这里如果返回true的话,touch事件将被拦截
// 拦截后 PopupWindow的onTouchEvent不被调用,这样点击外部区域无法dismiss
}
});*斜体文字*
那么问题来了。我一点击显示popupwindows,数据一直是null,有什么办法能让pop得到数据呢,
本来我加个了sleep,结果不行、对这些还是不了解,sleep时,是不是接受线程停止了,有什么办法能显示数据?求助
阿神2017-04-17 17:58:49
Sesetengah orang tidak memahami logik dari mana OutStream berasal. Jika anda ingin memaparkan data dalam urutan dalam masa nyata pada popupWindow, adalah betul untuk menggunakan pemegang untuk membuang data, tetapi mengapakah paparan popupWindow yang berkaitan dengan strim Tidak Adakah cukup untuk menetapkan semula nilai pada popupWindow? (Serupa dengan memberikan data dalam pemegang terus ke popWindow's textView.setText())
PHP中文网2017-04-17 17:58:49
Urut utama mengemas kini ui, mendapatkan data dan mengemas kini ui melalui panggilan balik