实例二
MainActivity
package com.example.test02;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Switch;
import android.widget.Toast;
public class MainActivity extends Activity {
EditText et_birthday;
private SharedPreferences sp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sp = getSharedPreferences("listDate", MODE_PRIVATE);
et_birthday = findViewById(R.id.et_birthday);
et_birthday.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
String time = year + "-" + (monthOfYear + 1) + "-" + dayOfMonth;
et_birthday.setText(time);
}
}
, 2021, 10, 10).show();
}
});
Button bt_submit = findViewById(R.id.bt_submit);
bt_submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SharedPreferences.Editor editor = sp.edit();
StringBuilder sb = new StringBuilder();
EditText et_username = findViewById(R.id.et_username);
sb.append("用户名:" + et_username.getText());
editor.putString("name", et_username.getText().toString());
EditText et_password = findViewById(R.id.et_password);
sb.append(",口令:" + et_password.getText());
editor.putString("pass", et_password.getText().toString());
RadioGroup rg_major = findViewById(R.id.rg_major);
RadioButton rb = findViewById(rg_major.getCheckedRadioButtonId());
sb.append(",专业:" + rb.getText());
editor.putString("major", rb.getText().toString());
sb.append(",爱好:");
CheckBox cb = findViewById(R.id.cb_net);
if (cb.isChecked()) {
sb.append(cb.getText());
editor.putString("hobby1", cb.getText().toString());
} else {
editor.putString("hobby1", "empty");
}
cb = findViewById(R.id.cb_sports);
if (cb.isChecked()) {
sb.append(cb.getText());
editor.putString("hobby2", cb.getText().toString());
} else {
editor.putString("hobby2", "empty");
}
cb = findViewById(R.id.cb_reading);
if (cb.isChecked()) {
sb.append(cb.getText());
editor.putString("hobby3", cb.getText().toString());
} else {
editor.putString("hobby3", "empty");
}
sb.append(",出生日期:" + et_birthday.getText().toString());
editor.putString("birthday", et_birthday.getText().toString());
Switch switch1=(Switch)findViewById(R.id.switch1);
String s=switch1.isChecked()?"已激活":"未激活";
sb.append(",激活与否:" + s);
editor.putString("sw",s);
Toast.makeText(MainActivity.this, sb.toString(), Toast.LENGTH_LONG).show();
editor.commit();
}
});
Button bt_next = (Button) findViewById(R.id.bt_next);
bt_next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent();
intent.setClass(MainActivity.this, SecondActivity.class);
startActivity(intent);
MainActivity.this.finish();
}
});
}
}
SecondActivity.java
package com.example.test02;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
public class SecondActivity extends Activity {
EditText et_birthday;
private SharedPreferences sp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
sp = getSharedPreferences("listDate", MODE_PRIVATE);
Button bt_read = (Button) findViewById(R.id.bt_read);
bt_read.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String name = sp.getString("name", "");
String pass = sp.getString("pass", "");
String major = sp.getString("major", "");
String hobby1 = sp.getString("hobby1", "");
String hobby2 = sp.getString("hobby2", "");
String hobby3 = sp.getString("hobby3", "");
String birthday = sp.getString("birthday", "");
String sw = sp.getString("sw", "");
TextView tv_username = findViewById(R.id.tv_username);
tv_username.setText("账号:"+name);
TextView tv_password = findViewById(R.id.tv_password);
tv_password.setText("密码:"+pass);
TextView tv_job = findViewById(R.id.tv_job);
tv_job.setText(name);
if (major.equals("软件")) {
tv_job.setText("专业:"+"软件");
} else if (major.equals("物联网")) {
tv_job.setText("专业:"+"物联网");
} else if (major.equals("计算机")) {
tv_job.setText("专业:"+"计算机");
} else if (major.equals("大数据")) {
tv_job.setText("专业:"+"大数据");
}
CheckBox cb_net = (CheckBox) findViewById(R.id.cb_net);
CheckBox cb_sports = (CheckBox) findViewById(R.id.cb_sports);
CheckBox cb_reading = (CheckBox) findViewById(R.id.cb_reading);
if (hobby1.equals("上网")) {
cb_net.setChecked(true);
}
if (hobby2.equals("运动")) {
cb_sports.setChecked(true);
}
if (hobby3.equals("读书")) {
cb_reading.setChecked(true);
}
TextView tv_birthday = findViewById(R.id.tv_birthday);
tv_birthday.setText("出生日期:"+birthday);
TextView switch1 = findViewById(R.id.tv_switch);
switch1.setText("激活与否:"+sw);
}
});
Button bt_next = (Button) findViewById(R.id.bt_next);
bt_next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent();
intent.setClass(SecondActivity.this,MainActivity.class);
startActivity(intent);
SecondActivity.this.finish();
}
});
}
}
activity_main.xml
<?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"
tools:context=".MainActivity">
<EditText
android:id="@+id/et_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入用户名" />
<EditText
android:id="@+id/et_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入口令"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="专业"/>
<RadioGroup
android:id="@+id/rg_major"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/rd_soft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="软件" />
<RadioButton
android:id="@+id/rd_net"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="物联网" />
<RadioButton
android:id="@+id/rd_computer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="计算机" />
<RadioButton
android:id="@+id/rd_info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="大数据" />
</RadioGroup>
<TextView
android:id="@+id/tv_major"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="爱好"/>
<CheckBox
android:id="@+id/cb_net"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="上网" />
<CheckBox
android:id="@+id/cb_sports"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="运动" />
<CheckBox
android:id="@+id/cb_reading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="读书" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="出生日期"/>
<EditText
android:id="@+id/et_birthday"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName" />
<Switch
android:id="@+id/switch1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="激活状态" />
<Button
android:id="@+id/bt_submit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="保存数据到文件" />
<Button
android:id="@+id/bt_next"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="下一页" />
</LinearLayout>
second.xml
<?xml version="1.0" encoding="utf-8"?>
<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/tv_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:text="账号:"
android:textSize="18sp" />
<TextView
android:id="@+id/tv_password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:text="密码:"
android:textSize="18sp" />
<TextView
android:id="@+id/tv_job"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:text="专业:"
android:textSize="18sp" />
<TextView
android:id="@+id/tv_major"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="爱好:"
android:textSize="18sp" />
<CheckBox
android:id="@+id/cb_net"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="上网" />
<CheckBox
android:id="@+id/cb_sports"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="运动" />
<CheckBox
android:id="@+id/cb_reading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="读书" />
<TextView
android:id="@+id/tv_birthday"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="出生日期:" />
<TextView
android:id="@+id/tv_switch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="激活与否:" />
<Button
android:id="@+id/bt_read"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="从文件读取数据" />
<Button
android:id="@+id/bt_next"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="上一页" />
</LinearLayout>
|