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

android信息登记,页面跳转及文件存储

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

android信息登记,页面跳转及文件存储

实例二 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;
    //SharedPreferences对象,用于轻量级数据存储
    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();
                //创建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());
                //getCheckedRadioButtonId 得到被cheked的按钮的id
                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 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;
    //SharedPreferences对象,用于轻量级数据存储
    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("软件")) {//不能直接用major=="软件"
                    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 intent = new Intent();
                intent.setClass(SecondActivity.this,MainActivity.class);
                startActivity(intent);

                //如果不关闭当前的会出现好多个页面
                SecondActivity.this.finish();
            }
        });
    }
}
activity_main.xml



        

        

        

        

            
            
            

            
        
        

    

        

        

    

    

    

    
second.xml



    

    

    

    

        

        

        

    

    

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

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

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