Android 初识数据库存储
2016-08-12 12:30
246 查看
[b] 1.点击自动登录或者记住密码,点击登录跳转到另一个页面,另一个页面的switch的状态同主界面checkbox的状态相同。 [/b]
2.设置一个简单的登录页面,输入用户名和密码,点击登录跳转到另一个页面,在另一个页面中点击保存出现输入的用户名和密码。
第一个主界面布局及代码:
布局:
布局效果:
代码:
第一个跳转界面布局及代码:
布局:
第二个主界面布局及代码:
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); } }); } }完成效果:
相关文章推荐
- 【Android游戏开发十三】(保存游戏数据 [下文])详解SQLite存储方式,并把SQLite的数据库文件存储在SD卡中!!!
- 传智播客—Android(三)数据存储之三SQLite嵌入式数据库
- Android游戏开发13:SQLite存储数据及保存SQLite数据库文件到SD卡
- Android数据存储初识
- android SQLite数据库 一次性存储多条数据
- Android 数据存储之 SQLite嵌入式数据库
- Android 数据库存储之db4o
- android SQLite数据库 一次性存储多条数据
- Android中数据存储--采用SQLite存储数据及在SDCard中创建数据库
- android(16)_数据存储与访问_SQLite数据库_数据库的创建管理
- Android中数据存储--采用SQLite存储数据及在SDCard中创建数据库
- 【Android游戏开发十三】(保存游戏数据 [下文])详解SQLite存储方式,并把SQLite的数据库文件存储在SD卡中!!!
- android数据库存储方法(一)---------------SharedPreferences(实例)记录应用程序使用次数(二)读写写他应用程序
- Android本地数据存储之SQLite关系型数据库 ——SQLiteDatabase
- 【Android游戏开发十三】(保存游戏数据 [下文])详解SQLite存储方式,并把SQLite的数据库文件存储在SD卡中!!!
- Android多种数据库存储方式对比
- android数据库存储方法(一)---------------SharedPreferences(实例)记录应用程序使用次数(一)
- Android系统数据库初识
- android 这篇文章让我更加明白android数据库存储
- android(17)_数据存储与访问_SQLite数据库_修改数据库