数据存储--SharedPreferences--仿QQ登陆界面记住密码效果

数据存储--SharedPreferences--仿QQ登陆界面记住密码效果

SharedPreferences:共享数据存储

实现效果:1 用户输入用户名和密码,点击记住密码时,记住此用户名和密码,否则报空

              2 用户点击登陆时,首先判断是否记住密码,是则弹出用户名和密码

布局文件

<LinearLayout 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"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/l1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="姓名"
            android:textSize="18sp" />

        <EditText
            android:id="@+id/edit_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>

  <LinearLayout
        android:id="@+id/l2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/password"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="密码"
            android:textSize="18sp" />

        <EditText
            android:id="@+id/edit_password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textPassword" />

        <requestFocus />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/l3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/login"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="登陆"
            android:textSize="18sp" />
    </LinearLayout>

    <CheckBox
        android:id="@+id/save"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="记住密码"
        android:textSize="18sp" />

</LinearLayout>


逻辑代码
<span style="font-family:Comic Sans MS;font-size:18px;">package com.example.week3_day3_login;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {

	//声明控件
	private EditText name;
	private EditText password;
	private Button login;
	private CheckBox save;
	private String names;
	private String passwords;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		//找到控件id
		name = (EditText) findViewById(R.id.edit_name);
		password = (EditText) findViewById(R.id.edit_password);
		login = (Button) findViewById(R.id.login);
		save = (CheckBox) findViewById(R.id.save);
		login.setOnClickListener(this);
		save.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		// 得到用户名和密码
		names = name.getText().toString().trim();
		passwords = password.getText().toString().trim();
		switch (v.getId()) {
		//点击登陆按钮时
		case R.id.login:
			if (!save.isChecked()) {
				Toast.makeText(getApplicationContext(), "没有记住用户名和密码",
						Toast.LENGTH_SHORT).show();
			} else {
				//取出数据
				SharedPreferences preferences2 = getPreferences(Context.MODE_PRIVATE);
				String name2 = preferences2.getString("names", "default");
				String password2 = preferences2.getString("passwords",
						"default");
				Toast.makeText(MainActivity.this,
						"用户名:" + name2 + "\n密码:" + password2,
						Toast.LENGTH_SHORT).show();
			}
			break;
			//点击记住密码按钮
		case R.id.save:
			if (names.equals("") || passwords.equals("")) {
				Toast.makeText(getApplicationContext(), "用户名或密码不能为空",
						Toast.LENGTH_SHORT).show();
			} else {
				//保存数据
				SharedPreferences preferences = getPreferences(Context.MODE_PRIVATE);
				Editor editor = preferences.edit();//获得编辑者对象
				editor.putString("names", names);
				editor.putString("passwords", passwords);
				editor.commit();//提交
				Toast.makeText(MainActivity.this, "记住了密码和用户名",
						Toast.LENGTH_SHORT).show();
			}
			break;
		default:
			break;
		}

	}
}
</span>


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值