andriod studio 文件数据存储(存储姓名,学号,数学,语文)代码及详细步骤
时间: 2024-05-06 09:19:52 浏览: 18
在Android Studio中,可以通过以下步骤来实现文件数据存储:
1. 创建一个新的Android项目。
2. 在项目中创建一个Activity,用于显示数据。
3. 在布局文件中添加EditText控件,用于输入姓名、学号、数学、语文成绩。
4. 在Activity中获取EditText控件的值,并将其保存到SharedPreferences中。
5. 在Activity中读取SharedPreferences中的数据,并将其显示在TextView控件中。
下面是完整的代码实现:
1. 在布局文件中添加EditText控件
```
<EditText
android:id="@+id/edit_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="姓名"/>
<EditText
android:id="@+id/edit_number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="学号"/>
<EditText
android:id="@+id/edit_math"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:hint="数学成绩"/>
<EditText
android:id="@+id/edit_chinese"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:hint="语文成绩"/>
```
2. 在Activity中获取EditText控件的值,并将其保存到SharedPreferences中。
```
public class MainActivity extends AppCompatActivity {
private EditText editName;
private EditText editNumber;
private EditText editMath;
private EditText editChinese;
private SharedPreferences sharedPreferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editName = findViewById(R.id.edit_name);
editNumber = findViewById(R.id.edit_number);
editMath = findViewById(R.id.edit_math);
editChinese = findViewById(R.id.edit_chinese);
sharedPreferences = getSharedPreferences("data", Context.MODE_PRIVATE);
findViewById(R.id.btn_save).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = editName.getText().toString();
String number = editNumber.getText().toString();
String math = editMath.getText().toString();
String chinese = editChinese.getText().toString();
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("name", name);
editor.putString("number", number);
editor.putString("math", math);
editor.putString("chinese", chinese);
editor.apply();
Toast.makeText(MainActivity.this, "保存成功", Toast.LENGTH_SHORT).show();
}
});
}
}
```
3. 在Activity中读取SharedPreferences中的数据,并将其显示在TextView控件中。
```
public class MainActivity extends AppCompatActivity {
private TextView textName;
private TextView textNumber;
private TextView textMath;
private TextView textChinese;
private SharedPreferences sharedPreferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textName = findViewById(R.id.text_name);
textNumber = findViewById(R.id.text_number);
textMath = findViewById(R.id.text_math);
textChinese = findViewById(R.id.text_chinese);
sharedPreferences = getSharedPreferences("data", Context.MODE_PRIVATE);
String name = sharedPreferences.getString("name", "");
String number = sharedPreferences.getString("number", "");
String math = sharedPreferences.getString("math", "");
String chinese = sharedPreferences.getString("chinese", "");
textName.setText(name);
textNumber.setText(number);
textMath.setText(math);
textChinese.setText(chinese);
}
}
```
以上就是在Android Studio中实现文件数据存储的详细步骤和代码。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)