private EditText ipEditText; private EditText portEditText; private TextView messageTextView; private BufferedReader in; private PrintWriter out; private Socket socket; private Handler handler = new Handler(); @SuppressLint("MissingInflatedId") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ipEditText = findViewById(R.id.ipEditText); portEditText = findViewById(R.id.portEditText); Button connectButton = findViewById(R.id.connectButton); connectButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String ip = ipEditText.getText().toString(); int port = Integer.parseInt(portEditText.getText().toString()); new ConnectTask().execute(ip, String.valueOf(port)); } }); Button sendMessageButton = findViewById(R.id.sendMessageButton); sendMessageButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { showSendMessageDialog(); } }); } private class ConnectTask extends AsyncTask<String, Void, Void> { @Override protected Void doInBackground(String... params) { String ip = params[0]; int port = Integer.parseInt(params[1]); try { socket = new Socket(ip, port); in = new BufferedReader(new InputStreamReader(socket.getInputStream())); out = new PrintWriter(socket.getOutputStream(), true); // 获取SeekBar对象 SeekBar progressBar = findViewById(R.id.progressBar); // 添加OnSeekBarChangeListener监听器 progressBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { // 计算百分比对应的数字 int number = progress * 10; int tnum = 0; tnum = number; out.println(tnum); } @Override public void onStartTrackingTouch(SeekBar seekBar) {} @Override public void onStopTrackingTouch(SeekBar seekBar) {} }); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } } @Override protected void onDestroy() { super.onDestroy(); try { if (in != null) { in.close(); } if (out != null) { out.close(); } if (socket != null) { socket.close(); } } catch (IOException e) { e.printStackTrace(); } } private void showSendMessageDialog() { AlertDialog.Builder builder = new AlertDialog.Builder(this); LayoutInflater inflater = getLayoutInflater(); View view = inflater.inflate(R.layout.dialog_send_message, null); final EditText messageEditText = view.findViewById(R.id.messageEditText); builder.setView(view) .setTitle("发送消息") .setPositiveButton("发送", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { String message = messageEditText.getText().toString(); if (out != null) { new Thread(new Runnable() { @Override public void run() { out.println(message); } }).start(); } } }) .setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.cancel(); } }) .create() .show(); }代码有问题,修改一下
时间: 2024-02-25 16:58:29 浏览: 74
EditText与TextView 属性大全
这段代码的问题在于:
1. `onPostExecute()` 方法没有被实现。
2. `ProgressBar` 的更新应该在 UI 线程中进行。
3. 在 `AsyncTask` 中创建 `SeekBar` 的操作应该在 UI 线程中进行。
下面是修改后的代码:
```
private EditText ipEditText;
private EditText portEditText;
private TextView messageTextView;
private BufferedReader in;
private PrintWriter out;
private Socket socket;
private Handler handler = new Handler();
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ipEditText = findViewById(R.id.ipEditText);
portEditText = findViewById(R.id.portEditText);
Button connectButton = findViewById(R.id.connectButton);
connectButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String ip = ipEditText.getText().toString();
int port = Integer.parseInt(portEditText.getText().toString());
new ConnectTask().execute(ip, String.valueOf(port));
}
});
Button sendMessageButton = findViewById(R.id.sendMessageButton);
sendMessageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showSendMessageDialog();
}
});
}
private class ConnectTask extends AsyncTask<String, Void, Void> {
@Override
protected Void doInBackground(String... params) {
String ip = params[0];
int port = Integer.parseInt(params[1]);
try {
socket = new Socket(ip, port);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(), true);
// 在 UI 线程中创建 SeekBar
handler.post(new Runnable() {
@Override
public void run() {
SeekBar progressBar = findViewById(R.id.progressBar);
progressBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
// 计算百分比对应的数字
int number = progress * 10;
int tnum = 0;
tnum = number;
// 在 UI 线程中更新 ProgressBar 的进度
handler.post(new Runnable() {
@Override
public void run() {
seekBar.setProgress(progress);
}
});
out.println(tnum);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {}
});
}
});
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
// 在连接结束后做一些处理
// 比如关闭连接或者显示连接成功的提示
}
}
@Override
protected void onDestroy() {
super.onDestroy();
try {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
if (socket != null) {
socket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
private void showSendMessageDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
LayoutInflater inflater = getLayoutInflater();
View view = inflater.inflate(R.layout.dialog_send_message, null);
final EditText messageEditText = view.findViewById(R.id.messageEditText);
builder.setView(view)
.setTitle("发送消息")
.setPositiveButton("发送", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String message = messageEditText.getText().toString();
if (out != null) {
new Thread(new Runnable() {
@Override
public void run() {
out.println(message);
}
}).start();
}
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
})
.create()
.show();
}
```
阅读全文