您的位置:首页 > 移动开发 > Android开发

Android 初识数据库存储

2016-08-12 12:30 246 查看
[b]  1.点击自动登录或者记住密码,点击登录跳转到另一个页面,另一个页面的switch的状态同主界面checkbox的状态相同。 [/b]

 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 数据库
相关文章推荐