android

日历:

 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);

            }
        });
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Andrewlong_lhl

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值