package com.example.ksswj; import androidx.appcompat.app.AppCompatActivity; import android.annotation.SuppressLint; import android.os.Bundle; import android.os.Handler; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.Socket; import java.net.UnknownHostException; public class MainActivity extends AppCompatActivity { private EditText ipEditText; private EditText portEditText; private TextView messageTextView; private BufferedReader in; 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); messageTextView = findViewById(R.id.messageTextView); 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 Thread(new Runnable() { @Override public void run() { try { socket = new Socket(ip, port); in = new BufferedReader(new InputStreamReader(socket.getInputStream())); // 在主页面中的窗口中实时显示消息 while (true) { String message = in.readLine(); if (message == null) { break; } handler.post(new Runnable() { @Override public void run() { messageTextView.append(message + "\n"); } }); } } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }).start(); } }); } @Override protected void onDestroy() { super.onDestroy(); try { if (in != null) { in.close(); } if (socket != null) { socket.close(); } } catch (IOException e) { e.printStackTrace(); } } }我还需要在上面代码中加入一个对话框,能够向端口发送消息,能帮我加上代码吗
时间: 2024-02-22 16:57:02 浏览: 93
当然可以,你需要在 layout 文件夹下创建一个新的 xml 文件作为对话框的布局,比如命名为 dialog_send_message.xml。然后在 MainActivity 中添加一个方法,用来弹出对话框并向端口发送消息。代码如下:
```java
package com.example.ksswj;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
public class MainActivity extends AppCompatActivity {
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);
messageTextView = findViewById(R.id.messageTextView);
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 Thread(new Runnable() {
@Override
public void run() {
try {
socket = new Socket(ip, port);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(), true);
// 在主页面中的窗口中实时显示消息
while (true) {
String message = in.readLine();
if (message == null) {
break;
}
handler.post(new Runnable() {
@Override
public void run() {
messageTextView.append(message + "\n");
}
});
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
});
Button sendMessageButton = findViewById(R.id.sendMessageButton);
sendMessageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showSendMessageDialog();
}
});
}
@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) {
out.println(message);
}
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
})
.create()
.show();
}
}
```
其中,`showSendMessageDialog()` 方法用来弹出对话框,`dialog_send_message.xml` 文件作为对话框的布局文件。在对话框中,用户可以输入要发送的消息,然后点击“发送”按钮将消息发送到端口上。
阅读全文