我无法连接按钮2请告诉我该怎么做。我从不这样做><我可以点击按钮1按钮2,但它只显示id button1。
package com.app.messageclient;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MessageClient extends Activity implements OnClickListener {
Button button1;
Button button2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.messageclient);
button2=(Button)findViewById(R.id.button2);
button2.setOnClickListener(this);
button1=(Button)findViewById(R.id.button1);
button1.setOnClickListener(this);
}
我连接按钮1,该代码与此插座连接(“10.2.114.192”,8080)
public void onClick(View v) {
// TODO Auto-generated method stub
Thread t=new Thread(){
public void run() {
// TODO Auto-generated method stub
try {
Socket S=new Socket("10.2.114.192",8080);
DataOutputStream dos=new DataOutputStream(S.getOutputStream());
dos.writeUTF(button1.getText().toString());
dos.flush();
dos.close();
S.close();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
t.start();
Toast.makeText(this,"This message sent!", Toast.LENGTH_SHORT).show();
}
我可以写它来做吗?
//Button2
public void onClick1(View v) {
// TODO Auto-generated method stub
Thread t=new Thread(){
public void run() {
// TODO Auto-generated method stub
try {
Socket S=new Socket("10.2.114.192",8080);
DataOutputStream dos=new DataOutputStream(S.getOutputStream());
dos.writeUTF(button2.getText().toString());
dos.flush();
dos.close();
S.close();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
t.start();
Toast.makeText(this,"This message sent!", Toast.LENGTH_SHORT).show();
}
}