栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

android

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

            }
        });
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/459111.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号