您是编程新手。我正在使用onClick(View v)启动一个线程。它运行正常。现在我想重复“按钮点击”而不再点击按钮。我找了大约5个小时。但我没有得到任何答案。我用户button.performClick();但什么都没发生。我想重新开始我的线程。
接收器:
private OnClickListener connectListener = new OnClickListener() {
@Override
public void onClick(View v) {
if (!connected) {
serverIpAddress = serverIp.getText().toString();
if (!serverIpAddress.equals("")) {
Thread cThread = new Thread(new ClientThread());
cThread.start();
connectPhones = (Button) findViewById(R.id.connect_phones);
connectPhones.setPressed(false);
connectPhones.invalidate();
Handler handler1 = new Handler();
Runnable r1 = new Runnable() {
public void run() {
connectPhones.performClick();
connectPhones.setPressed(true);
connectPhones.invalidate();
connectPhones.setPressed(false);
connectPhones.invalidate();
}
};
handler1.postDelayed(r1, 1000);
}
}
}
};
public class ClientThread implements Runnable {
public void run() {
try {
InetAddress serverAddr = InetAddress.getByName(serverIpAddress);
Log.d("ClientActivity", "C: Connecting...");
Socket socket = new Socket(serverAddr, ServerActivity.SERVERPORT);
connected = true;
final String path = "/sdcard/all.png";
File file = new File(path);
ObjectInputStream reader = new ObjectInputStream(socket.getInputStream());
int nRead = 0;
byte[] data = new byte[socket.getReceiveBufferSize()];
FileOutputStream fileOutputStream = new FileOutputStream( path );
// while(connected == true){
while( (nRead = reader.read(data)) != -1 ){
fileOutputStream.write( data, 0, nRead );
fileOutputStream.flush();
data = new byte[socket.getReceiveBufferSize()];
}
fileOutputStream.close();
//}
handler.post(new Runnable() {
@Override
public void run() {
Bitmap bitmap = BitmapFactory.decodeFile(path);
image.setImageBitmap(bitmap);
image.postInvalidate();
}
});
file.delete();
reader.close();
//connectPhones.performClick();
/* while (connected) {
try {
Log.d("ClientActivity", "C: Sending command.");
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket
.getOutputStream())), true);
// WHERE YOU ISSUE THE COMMANDS
out.println("Hey Server!");
Log.d("ClientActivity", "C: Sent.");
} catch (Exception e) {
Log.e("ClientActivity", "S: Error", e);
}
}*/
socket.close();
Log.d("ClientActivity", "C: Closed.");
} catch (Exception e) {
Log.e("ClientActivity", "C: Error", e);
}
connected = false;
}
}
}
答案 0 :(得分:0)
在onClick
方法中添加此内容:
btn = (Button) findViewById(R.id.yourButton);
btn.setPressed(false);
btn.invalidate();
Handler handler1 = new Handler();
Runnable r1 = new Runnable() {
public void run() {
btn.performClick();
btn.setPressed(true);
btn.invalidate();
btn.setPressed(false);
btn.invalidate();
}
};
handler1.postDelayed(r1, 1000);
答案 1 :(得分:0)
为了您的目的,我创建了一个示例代码。这段代码完美无缺。你可以试试这个。您可以在下面编写代码//在此处下载文件评论。
public class MainActivity extends Activity {
boolean connected = false;
Button button_example = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button_example = (Button) findViewById(R.id.button1);
button_example.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "button clicked called", Toast.LENGTH_SHORT).show();
System.out.println("bb message called");
if (!connected) {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
connected = true;
// DOWNLOAD FILE HERE.
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
runOnUiThread(new Runnable() {
public void run() {
connected = false;
button_example.performClick();
}
});
}
});
thread.start();
}
}
});
}