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(); } }能为我写出上面代码的布局文件吗
时间: 2024-02-22 15:57:09 浏览: 53
当然可以!下面是该代码中使用到的布局文件:
activity_main.xml:
```xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/ipEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入服务器IP地址" />
<EditText
android:id="@+id/portEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入服务器端口号" />
<Button
android:id="@+id/connectButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="连接" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<TextView
android:id="@+id/messageTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#000000" />
</ScrollView>
<Button
android:id="@+id/sendMessageButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="发送消息" />
</LinearLayout>
```
dialog_send_message.xml:
```xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">
<EditText
android:id="@+id/messageEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入要发送的消息" />
</LinearLayout>
```
阅读全文