3-29打卡
sharedperference
• 所花时间:5
• 代码行数:121
• 博客容量:1
• 代码如下:
package com.example.chapter07;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Toast;
public class sharedPreference extends AppCompatActivity implements View.OnClickListener {
private EditText et_age;
private EditText et_class;
private EditText et_name;
private RadioGroup rg_sex;
private SharedPreferences preferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shared_preference);
et_age=findViewById(R.id.et_age);
et_class=findViewById(R.id.et_class);
et_name=findViewById(R.id.et_name);
rg_sex=findViewById(R.id.rg_sex);
findViewById(R.id.bt_submit).setOnClickListener(this);
preferences = getSharedPreferences("config", MODE_PRIVATE);
reload();
}
private void reload() {
String name =preferences.getString("name",null);
int age=preferences.getInt("age",0);
String sClass =preferences.getString("class",null);
String sex=preferences.getString("sex",null);
if(name!=null){
et_name.setText(name);
}
if(age!=0){
et_age.setText(String.valueOf(age));
}
if(sex!=null){
rg_sex.check(sex.equals("男")?R.id.rb_male:R.id.rb_female);
}
if(sClass!=null){
et_class.setText(sClass);
}
}
@Override
public void onClick(View v) {
String name=et_name.getText().toString();
String age=et_age.getText().toString();
String sClass=et_class.getText().toString();
String sex=rg_sex.getCheckedRadioButtonId()==(R.id.rb_female)?"女":"男";
SharedPreferences.Editor edit = preferences.edit();
edit.putString("name",name);
edit.putInt("age", Integer.valueOf(age));
edit.putString("class",sClass);
edit.putString("sex",sex);
boolean res=edit.commit();
if(res){
Toast toast = Toast.makeText(this, "提交成功", Toast.LENGTH_SHORT);
toast.show();
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="姓 名:"
android:textSize="17sp"
/>
<EditText
android:id="@+id/et_name"
android:layout_width="0dp"
android:layout_weight="2"
android:layout_height="wrap_content"
android:hint="请输入姓名"
android:inputType="text"
android:background="@drawable/focusedit"
android:maxLength="10"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="年 龄:"
android:textSize="17sp"
/>
<EditText
android:id="@+id/et_age"
android:layout_width="0dp"
android:layout_weight="2"
android:layout_height="wrap_content"
android:hint="请输入年龄:"
android:inputType="number"
android:background="@drawable/focusedit"
android:maxLength="5"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="专 业:"
android:textSize="17sp"
android:maxLength="15"
/>
<EditText
android:id="@+id/et_class"
android:layout_width="0dp"
android:layout_weight="2"
android:layout_height="wrap_content"
android:hint="请输入专业"
android:inputType="text"
android:background="@drawable/focusedit"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:text="性 别:"
android:textSize="17sp"
/>
<RadioGroup
android:id="@+id/rg_sex"
android:layout_weight="1.3"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<RadioButton
android:id="@+id/rb_male"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="男"
/>
<RadioButton
android:id="@+id/rb_female"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="女"
/>
</RadioGroup>
</LinearLayout>
<Button
android:id="@+id/bt_submit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="提交"
/>
</LinearLayout>
浙公网安备 33010602011771号