Android 初识数据库存储

  1.点击自动登录或者记住密码,点击登录跳转到另一个页面,另一个页面的switch的状态同主界面checkbox的状态相同。 

 2.设置一个简单的登录页面,输入用户名和密码,点击登录跳转到另一个页面,在另一个页面中点击保存出现输入的用户名和密码。

  

  第一个主界面布局及代码:

布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".SharedPreferencesActivity">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="登录"
        android:id="@+id/save"/>
    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="记住密码"
        android:id="@+id/cb2"
        android:layout_below="@+id/save" />
    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="自动登录"
        android:id="@+id/cb1"
        android:layout_below="@+id/save" />
</RelativeLayout>

布局效果:


  代码:

package com.example.administrator.jreduch09;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;

public class SharedPreferencesActivity extends AppCompatActivity {
    private Button save;
    private CheckBox cb1;
    private CheckBox cb2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        save=(Button)findViewById(R.id.save);
        cb1= (CheckBox) findViewById(R.id.cb1);
        cb2= (CheckBox) findViewById(R.id.cb2);

        final SharedPreferences sp=getSharedPreferences("userInfo",MODE_PRIVATE);
        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               SharedPreferences.Editor editor =sp.edit();
                editor.putBoolean("1",cb1.isChecked());
                editor.putBoolean("2",cb2.isChecked());
                editor.commit();
                Intent intent=new Intent(SharedPreferencesActivity.this,SettingsActivity.class);
                startActivity(intent);
            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        SharedPreferences sp=getSharedPreferences("switch",MODE_PRIVATE);
        boolean bb1=sp.getBoolean("3",false);
        boolean bb2=sp.getBoolean("4",false);
        cb1.setChecked(bb1);
        cb2.setChecked(bb2);
    }
}

 

第一个跳转界面布局及代码:

布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.administrator.jreduch09.SettingsActivity">
    <Switch
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="自动登录"
        android:id="@+id/switch1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="105dp" />

    <Switch
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="记住密码"
        android:id="@+id/switch2"
        android:layout_below="@+id/switch1"
        android:layout_centerHorizontal="true" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="保存"
        android:id="@+id/save"
        android:layout_below="@+id/switch2"
        android:layout_alignParentStart="true" />
</RelativeLayout>
代码:
package com.example.administrator.jreduch09;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Switch;

public class SettingsActivity extends AppCompatActivity {
    private Button save;
    private Switch switch1;
    private Switch switch2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);
        save= (Button) findViewById(R.id.save);
        switch1= (Switch) findViewById(R.id.switch1);
        switch2= (Switch) findViewById(R.id.switch2);
       final SharedPreferences sp=getSharedPreferences("switch",MODE_PRIVATE);

        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPreferences.Editor editor =sp.edit();
                editor.putBoolean("3",switch1.isChecked());
                editor.putBoolean("4",switch2.isChecked());
                editor.commit();
            }
        });
    }
    @Override
    protected void onStart() {
        super.onStart();
        SharedPreferences sp=getSharedPreferences("userInfo",MODE_PRIVATE);
        boolean b1=sp.getBoolean("1",true);
        boolean b2=sp.getBoolean("2", true);
        switch1.setChecked(b1);
        switch2.setChecked(b2);
    }
}
完成效果:


 第二个主界面布局及代码: 

布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".SharedPreferencesActivity">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入用户名"
        android:id="@+id/user"
        />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入密码 "
        android:id="@+id/pwd"
        android:layout_below="@+id/user"
        />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="登录"
        android:id="@+id/save"
        android:layout_below="@+id/pwd"
        />
</RelativeLayout>
代码:
package com.example.administrator.jreduch09;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;

public class SharedPreferencesActivity extends AppCompatActivity {
    private EditText user;
    private EditText pwd;
    private Button save;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        user= (EditText) findViewById(R.id.user);
        pwd= (EditText) findViewById(R.id.pwd);
        save=(Button)findViewById(R.id.save);

        final SharedPreferences sp=getSharedPreferences("userInfo",MODE_PRIVATE);
        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               SharedPreferences.Editor editor =sp.edit();
                editor.putString("userName", user.getText().toString());
                editor.putInt("userPwd", Integer.parseInt(pwd.getText().toString()));
                editor.commit();              
                Intent intent=new Intent(SharedPreferencesActivity.this,SettingsActivity.class);
                startActivity(intent);
            }
        });
    }
第二个跳转界面布局及代码:
布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    tools:context="com.example.administrator.jreduch09.Sp2Activity">
<TextView
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:id="@+id/showUser"
    />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/getUser"
        android:text="获取数据"
        android:layout_below="@+id/showUser"
        />
</RelativeLayout>
代码:
package com.example.administrator.jreduch09;

import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class Sp2Activity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sp2);
        Button getUser = (Button) findViewById(R.id.getUser);
        final TextView showUser= (TextView) findViewById(R.id.showUser);
        final SharedPreferences sp=getSharedPreferences("userInfo",MODE_PRIVATE);
        getUser.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name= sp.getString("userName","NULL");
                String pwd= String.valueOf(sp.getInt("userPwd", 8888));
                showUser.setText("用户名"+name+"\n"+"密码"+pwd);
            }
        });
    }
}
完成效果:


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值