帮我用android studio写一个app,我的需求是在主页面可以填入ip与端口,并且监听该端口下的消息,同时有两个半圆进度条,滑动进度条能够向端口发送对应的数字
时间: 2024-02-22 22:54:41 浏览: 20
好的,以下是一个简单的Android应用程序示例,可以在MainActivity上填写IP地址和端口号,然后通过socket监听该端口,并且可以通过两个半圆进度条向端口发送数字。
1. 首先,在Android Studio中创建一个新项目。
2. 然后,在MainActivity的布局文件中添加以下代码:
```xml
<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">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="IP地址" />
<EditText
android:id="@+id/ip_address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:text="192.168.1.1" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="端口号" />
<EditText
android:id="@+id/port_number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:text="8080" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="进度条1" />
<com.github.lzyzsd.circleprogress.ArcProgress
android:id="@+id/arc_progress1"
android:layout_width="match_parent"
android:layout_height="200dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="进度条2" />
<com.github.lzyzsd.circleprogress.ArcProgress
android:id="@+id/arc_progress2"
android:layout_width="match_parent"
android:layout_height="200dp" />
</LinearLayout>
```
3. 然后,在MainActivity中添加以下代码:
```java
public class MainActivity extends AppCompatActivity {
private EditText mIpAddressEditText;
private EditText mPortNumberEditText;
private ArcProgress mArcProgress1;
private ArcProgress mArcProgress2;
private Socket mSocket;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mIpAddressEditText = findViewById(R.id.ip_address);
mPortNumberEditText = findViewById(R.id.port_number);
mArcProgress1 = findViewById(R.id.arc_progress1);
mArcProgress2 = findViewById(R.id.arc_progress2);
mArcProgress1.setOnSeekArcChangeListener(new OnSeekArcChangeListener() {
@Override
public void onProgressChanged(SeekArc seekArc, int progress, boolean fromUser) {
if (mSocket != null && mSocket.isConnected()) {
try {
OutputStream outputStream = mSocket.getOutputStream();
outputStream.write(progress);
outputStream.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
public void onStartTrackingTouch(SeekArc seekArc) {
}
@Override
public void onStopTrackingTouch(SeekArc seekArc) {
}
});
mArcProgress2.setOnSeekArcChangeListener(new OnSeekArcChangeListener() {
@Override
public void onProgressChanged(SeekArc seekArc, int progress, boolean fromUser) {
if (mSocket != null && mSocket.isConnected()) {
try {
OutputStream outputStream = mSocket.getOutputStream();
outputStream.write(progress);
outputStream.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
public void onStartTrackingTouch(SeekArc seekArc) {
}
@Override
public void onStopTrackingTouch(SeekArc seekArc) {
}
});
new Thread(new Runnable() {
@Override
public void run() {
try {
String ipAddress = mIpAddressEditText.getText().toString();
int portNumber = Integer.parseInt(mPortNumberEditText.getText().toString());
mSocket = new Socket(ipAddress, portNumber);
InputStream inputStream = mSocket.getInputStream();
OutputStream outputStream = mSocket.getOutputStream();
while (true) {
byte[] buffer = new byte[1024];
int bytesRead = inputStream.read(buffer);
if (bytesRead != -1) {
String message = new String(buffer, 0, bytesRead);
Log.d("MainActivity", "Received message: " + message);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
}
```
4. 最后,添加以下依赖项:
```
implementation 'com.github.lzyzsd:circleprogress:1.2.1'
```
完成以上步骤后,运行应用程序,即可在主页面上填写IP地址和端口号,监听该端口,并且可以通过两个半圆进度条向端口发送数字。