帮我用android studio写一个app,我的需求是在主页面可以填入ip与端口,并且监听该端口下的消息,同时有两个半圆进度条,滑动进度条能够向端口发送对应的数字
时间: 2024-02-21 07:00:30 浏览: 66
好的,我可以给你提供一个简单的示例代码,你可以参考一下:
1. 创建一个新的Android Studio项目。
2. 打开activity_main.xml文件,在布局中添加两个半圆形进度条和一个EditText,一个Button,一个TextView,如下所示:
```
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ProgressBar
android:id="@+id/progressBar1"
style="?android:progressBarStyleHorizontal"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginTop="50dp"
android:layout_marginLeft="50dp"
android:progress="0"
android:secondaryProgress="100"
android:max="100"
android:indeterminate="false"
android:rotation="180"/>
<ProgressBar
android:id="@+id/progressBar2"
style="?android:progressBarStyleHorizontal"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginTop="50dp"
android:layout_marginRight="50dp"
android:progress="0"
android:secondaryProgress="100"
android:max="100"
android:indeterminate="false"/>
<EditText
android:id="@+id/editText"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="200dp"
android:hint="输入IP地址和端口号"/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="连接"
android:layout_below="@+id/editText"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"/>
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="接收到的消息:"
android:layout_below="@+id/button"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"/>
</RelativeLayout>
```
3. 打开MainActivity.java文件,添加以下代码:
```
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.InetSocketAddress;
import java.net.Socket;
public class MainActivity extends AppCompatActivity {
private EditText editText;
private Button button;
private TextView textView;
private ProgressBar progressBar1, progressBar2;
private String ip;
private int port;
private Socket socket;
private BufferedReader in;
private OutputStreamWriter out;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.editText);
button = (Button) findViewById(R.id.button);
textView = (TextView) findViewById(R.id.textView);
progressBar1 = (ProgressBar) findViewById(R.id.progressBar1);
progressBar2 = (ProgressBar) findViewById(R.id.progressBar2);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String[] temp = editText.getText().toString().split(":");
ip = temp[0];
port = Integer.parseInt(temp[1]);
new ConnectTask().execute();
}
});
progressBar1.setOnSeekBarChangeListener(new SeekBarListener());
progressBar2.setOnSeekBarChangeListener(new SeekBarListener());
}
private class ConnectTask extends AsyncTask<Void, String, Void> {
@Override
protected Void doInBackground(Void... params) {
try {
socket = new Socket();
socket.connect(new InetSocketAddress(ip, port), 5000);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new OutputStreamWriter(socket.getOutputStream());
publishProgress("连接成功");
while (true) {
String line = in.readLine();
if (line != null) {
publishProgress(line);
}
}
} catch (Exception e) {
publishProgress("连接失败: " + e.getMessage());
}
return null;
}
@Override
protected void onProgressUpdate(String... values) {
super.onProgressUpdate(values);
textView.setText(values[0]);
}
}
private class SeekBarListener implements ProgressBar.OnSeekBarChangeListener {
@Override
public void onProgressChanged(ProgressBar seekBar, int progress, boolean fromUser) {
if (socket != null && socket.isConnected()) {
try {
String message = String.valueOf(progress);
out.write(message);
out.flush();
} catch (Exception e) {
textView.setText("发送失败: " + e.getMessage());
}
}
}
@Override
public void onStartTrackingTouch(ProgressBar seekBar) {
}
@Override
public void onStopTrackingTouch(ProgressBar seekBar) {
}
}
}
```
这个例子实现了在主页面中输入IP地址和端口号,点击连接按钮后建立Socket连接,监听该端口下的消息,并且在TextView中显示接收到的消息。同时,你可以滑动半圆形进度条,向端口发送数字消息。
当然,这只是一个简单的示例,你可以根据自己的需求进行更改和扩展。
阅读全文