日历:
EditText et_csrq=findViewById(R.id.et_csrq);
et_csrq.setOnClickListener(new View.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public void onClick(View v) {
new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
// 此处得到选择的时间,可以进行你想要的操作
String time = year + "-" + (month + 1) + "-" + dayOfMonth;
et_csrq.setText(time);
}
}// 设置初始日期
, 2021, 10, 10).show();
}
});
保存:
Button bt_bc=findViewById(R.id.bt_bc);
bt_bc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
StringBuilder sb= new StringBuilder();
EditText et_username= findViewById(R.id.et_username);
sb.append("用户名:"+et_username.getText());
EditText et_password= findViewById(R.id.et_password);
sb.append(",口令:"+et_password.getText());
RadioGroup rg_zy= findViewById(R.id.rg_zy);
RadioButton rb=findViewById(rg_zy.getCheckedRadioButtonId());
sb.append(",专业:"+rb.getText());
String hobby="";
sb.append(",爱好:");
CheckBox cb = findViewById(R.id.cb_c);
if(cb.isChecked()){
sb.append(cb.getText()+",");
hobby+=cb.getText()+",";
}
cb =findViewById(R.id.cb_t);
if(cb.isChecked()) {
sb.append(cb.getText() + ",");
hobby += cb.getText() + ",";
}
cb = findViewById(R.id.cb_r);
if(cb.isChecked()) {
sb.append(cb.getText() + ",");
hobby += cb.getText() + ",";
}
cb = findViewById(R.id.cb_lq);
if(cb.isChecked()) {
sb.append(cb.getText() + ",");
hobby += cb.getText() + ",";
}
sb.append("出生日期:"+et_csrq.getText().toString());
SharedPreferences sharedPreferences=getSharedPreferences("file",MODE_PRIVATE);
SharedPreferences.Editor editor=sharedPreferences.edit();
editor.putString("username",et_username.getText().toString());
editor.putString("password",et_password.getText().toString());
editor.putString("zy",rb.getText().toString());
editor.putString("hobby",hobby);
editor.putString("birthday",et_csrq.getText().toString());
editor.commit();
Toast.makeText(MainActivity.this,sb.toString(), Toast.LENGTH_LONG).show();
}
});
读取:
Button bt_dq=findViewById(R.id.bt_dq);
bt_dq.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences sharedPreferences=getSharedPreferences("file",MODE_PRIVATE);
String username=sharedPreferences.getString("username","");
String password=sharedPreferences.getString("password","");
String zy=sharedPreferences.getString("zy","");
String hobby=sharedPreferences.getString("hobby","");
String birthday=sharedPreferences.getString("birthday","");
EditText et_username=findViewById(R.id.et_username);
et_username.setText(username);
EditText et_password= findViewById(R.id.et_password);
et_password.setText(password);
EditText et_birthday=findViewById(R.id.et_csrq);
et_birthday.setText(birthday);
RadioButton rb=findViewById(R.id.rb_rj);
if(rb.getText().toString().equals(zy)) {
rb.setChecked(true);
}
rb=findViewById(R.id.rb_dsj);
if(rb.getText().toString().equals(zy)) {
rb.setChecked(true);
}
rb=findViewById(R.id.rb_jsj);
if(rb.getText().toString().equals(zy)) {
rb.setChecked(true);
}
rb=findViewById(R.id.rb_wlw);
if(rb.getText().toString().equals(zy)) {
rb.setChecked(true);
}
String[] hobbies=hobby.split(",");
for (String h:hobbies) {
CheckBox cb=findViewById(R.id.cb_c);
if(cb.getText().toString().equals(Arrays.toString(hobbies))) {
cb.setChecked(true);
}
cb = findViewById(R.id.cb_t);
if(cb.getText().toString().equals(Arrays.toString(hobbies))) {
cb.setChecked(true);
}
cb=findViewById(R.id.cb_r);
if(cb.getText().toString().equals(Arrays.toString(hobbies))) {
cb.setChecked(true);
}
cb=findViewById(R.id.cb_lq);
if(cb.getText().toString().equals(Arrays.toString(hobbies))) {
cb.setChecked(true);
}
}
Toast.makeText(MainActivity.this,"数据读取成功!"+username+","+password+zy+hobby+birthday, Toast.LENGTH_LONG).show();
}
});
跳转:
Button bt_tz=findViewById(R.id.bt_tz);
bt_tz.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ComponentName componentName=new ComponentName(MainActivity.this,MainActivity2.class);
Intent intent=new Intent();
intent.setComponent(componentName);
startActivity(intent);
}
});
|