创建两个或两个以上的Activity,在他们之间进行数据的传递;并使用SharedPreference对象、File对象存储在Activity中输入的数据。展示其页面的详细代码
时间: 2024-06-17 14:03:18 浏览: 14
创建两个或两个以上的Activity,在它们之间传递数据,可以通过以下步骤实现:
1. 创建第一个Activity,定义需要传递的数据。
2. 在第一个Activity中使用Intent对象打开第二个Activity,并将需要传递的数据放入Intent对象中。
3. 在第二个Activity中使用getIntent()方法获取Intent对象,并使用getXXXExtra()方法获取传递过来的数据。
4. 在第二个Activity中,如果需要返回数据给第一个Activity,则可以在结束当前Activity时,将需要返回的数据放入Intent对象中,并使用setResult()方法设置返回结果。
5. 在第一个Activity中,使用onActivityResult()方法获取第二个Activity返回的结果。
使用SharedPreference对象和File对象存储输入的数据,可以通过以下步骤实现:
1. 在需要存储数据的Activity中,创建SharedPreferences对象,并使用edit()方法获取SharedPreferences.Editor对象。
2. 使用SharedPreferences.Editor对象的putXXX()方法存储输入的数据。
3. 调用SharedPreferences.Editor对象的commit()方法或apply()方法提交存储的数据。
4. 如果需要使用File对象存储输入的数据,则可以使用openFileOutput()方法获取FileOutputStream对象,并使用write()方法将数据写入文件中。
下面是一个展示页面详细代码的例子:
FirstActivity.java
```
public class FirstActivity extends AppCompatActivity {
private EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
editText = findViewById(R.id.edit_text);
Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String data = editText.getText().toString();
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.putExtra("data", data);
startActivityForResult(intent, 1);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1 && resultCode == RESULT_OK) {
String resultData = data.getStringExtra("resultData");
editText.setText(resultData);
}
}
public void saveData(String data) {
SharedPreferences.Editor editor = getSharedPreferences("data", MODE_PRIVATE).edit();
editor.putString("input_data", data);
editor.apply();
try {
FileOutputStream out = openFileOutput("data.txt", MODE_PRIVATE);
out.write(data.getBytes());
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
SecondActivity.java
```
public class SecondActivity extends AppCompatActivity {
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
textView = findViewById(R.id.text_view);
String data = getIntent().getStringExtra("data");
textView.setText(data);
Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String resultData = "result: " + textView.getText().toString();
Intent intent = new Intent();
intent.putExtra("resultData", resultData);
setResult(RESULT_OK, intent);
finish();
}
});
SharedPreferences preferences = getSharedPreferences("data", MODE_PRIVATE);
String input_data = preferences.getString("input_data", "");
textView.setText(input_data);
try {
FileInputStream in = openFileInput("data.txt");
byte[] buffer = new byte[in.available()];
in.read(buffer);
String input_data2 = new String(buffer);
textView.setText(input_data2);
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
activity_first.xml
```
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Input data"/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next"/>
</LinearLayout>
```
activity_second.xml
```
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Return"/>
</LinearLayout>
```
相关推荐
![](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)
![](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)