给出我所需的前端以及后端代码
时间: 2024-10-23 16:07:46 浏览: 21
当然可以。以下是根据文档内容整理出的前端和后端的关键代码示例:
### 前端(`MainActivity.java`)
```java
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private EditText ed_id;
private Button button;
private TextView show;
private ActivityResultLauncher<Intent> launcher;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ed_id = findViewById(R.id.ed_id);
button = findViewById(R.id.button);
show = findViewById(R.id.show);
// 注册 Activity 结果监听器
launcher = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
new ActivityResultCallback<ActivityResult>() {
@Override
public void onActivityResult(ActivityResult result) {
if (result.getResultCode() == 333) {
String show_number = result.getData().getStringExtra("number");
String show_value = result.getData().getStringExtra("value");
show.setText("设备号" + show_number + "返回的数据为:\n" + show_value);
}
}
});
// 按钮点击事件
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, Detail.class);
intent.putExtra("id", ed_id.getText().toString());
launcher.launch(intent);
}
});
}
}
```
### 后端(`Detail.java`)
```java
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class Detail extends AppCompatActivity {
private EditText ed_num;
private CheckBox shidu, co2, wendu, ph;
private Button bt_back;
private TextView t2_show;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
ed_num = findViewById(R.id.ed_num);
shidu = findViewById(R.id.shidu);
co2 = findViewById(R.id.co2);
wendu = findViewById(R.id.wendu);
ph = findViewById(R.id.ph);
bt_back = findViewById(R.id.bt_back);
t2_show = findViewById(R.id.t2_show);
// 获取传入的 ID
Intent intent = getIntent();
String id = intent.getStringExtra("id");
t2_show.setText(id + "你好!");
// 返回按钮点击事件
bt_back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (ed_num.getText().toString().isEmpty()) {
Toast.makeText(Detail.this, "设备号不能为空", Toast.LENGTH_SHORT).show();
} else {
Intent returnIntent = new Intent();
returnIntent.putExtra("number", ed_num.getText().toString());
StringBuilder valueBuilder = new StringBuilder();
if (shidu.isChecked()) valueBuilder.append(shidu.getText().toString());
if (co2.isChecked()) valueBuilder.append(co2.getText().toString());
if (wendu.isChecked()) valueBuilder.append(wendu.getText().toString());
if (ph.isChecked()) valueBuilder.append(ph.getText().toString());
returnIntent.putExtra("value", valueBuilder.toString());
setResult(333, returnIntent);
finish();
}
}
});
}
}
```
### 说明
- **前端 (`MainActivity.java`)**:
- 使用 `registerForActivityResult` 来注册和处理 Activity 结果。
- 当按钮被点击时,启动 `Detail` 活动并传递 `id`。
- 在 `onActivityResult` 方法中处理返回的数据,并更新 UI。
- **后端 (`Detail.java`)**:
- 接收从 `MainActivity` 传递过来的 `id` 并显示。
- 当返回按钮被点击时,检查输入是否为空,如果不为空则设置返回数据并关闭当前活动。
希望这些代码对你有帮助!如果有任何进一步的问题,请随时告诉我。
阅读全文