android 页面输入文字 点击按键跳转到其他页面 返回后 输入文字仍然保持显示 代码
时间: 2024-03-05 08:48:14 浏览: 28
你可以在 Android 中使用 startActivityForResult 方法来实现从一个页面跳转到另一个页面,并且保留数据。具体步骤如下:
1. 在当前页面中,使用 Intent 启动另一个页面:
```
Intent intent = new Intent(this, OtherActivity.class);
startActivityForResult(intent, REQUEST_CODE);
```
其中,REQUEST_CODE 是一个整型常量,用于标识当前请求。
2. 在另一个页面中,获取用户输入的数据,并在用户点击返回按钮时将数据返回给当前页面:
```
Intent intent = new Intent();
intent.putExtra("data", inputData);
setResult(RESULT_OK, intent);
finish();
```
其中,inputData 是用户输入的数据。
3. 在当前页面中,重写 onActivityResult 方法,获取从另一个页面返回的数据:
```
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
String inputData = data.getStringExtra("data");
// 将数据显示在输入框中
editText.setText(inputData);
}
}
```
在这个方法中,我们首先判断返回的 requestCode 是否等于我们在第一步中定义的 REQUEST_CODE,然后再判断 resultCode 是否等于 RESULT_OK,如果都成立,就可以从 Intent 中获取用户输入的数据,并将其显示在输入框中。
这样,当用户从另一个页面返回时,用户输入的数据就会被保留下来,并显示在输入框中。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)