package com.example.ksswj; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; 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; public class MainActivity extends AppCompatActivity { private EditText ipEditText; private EditText portEditText; private TextView messageTextView; private BufferedReader in; private Socket socket; @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()); try { socket = new Socket(ip, port); in = new BufferedReader(new InputStreamReader(socket.getInputStream())); // 在主页面中的窗口中实时显示消息 new Thread(new Runnable() { @Override public void run() { try { while (true) { String message = in.readLine(); if (message == null) { break; } runOnUiThread(new Runnable() { @Override public void run() { messageTextView.append(message + "\n"); } }); } } catch (IOException e) { e.printStackTrace(); } } }).start(); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }); } @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 11:56:19 浏览: 55
Android App Bundle .aab生成apk资源命令行
当然可以,以下是对应的布局文件代码:
```
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">
<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="连接" />
<TextView
android:id="@+id/messageTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp" />
</LinearLayout>
```
这是一个垂直的线性布局,包含两个 EditText、一个 Button 和一个 TextView。EditText 用于输入 IP 地址和端口号,Button 用于连接服务器,TextView 用于显示接收到的消息。
阅读全文