Android数据存储和访问

来源:互联网 发布:linux 退出sqlplus 编辑:程序博客网 时间:2024/05/17 08:47

1.下面是要求设计的完成界面:
这里写图片描述

2.登录界面代码如下:
添加一个login_top.xml编辑登录主界面实现代码如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="@drawble/login_top_roundbg"    >  <EditText      android:layout_width="match_parent"      android:layout_height="wrap_content"      android:id="@+id/etName"      android:background="@android:@drawable/etit_text"      android:drawablePadding="10dp"      android:drawableLeft="@drawable/icon_user"      android:ems="10"      android:hint="请输入账号">      </EditText>      <EditText          android:layout_width="match_parent"          android:layout_height="wrap_content"          android:background="@android:drawable/edit_textt"          android:layout_below="@id/etName"          android:id="@+id/etPassword"          android:drawableLeft="@drawable/icon_pass"          android:drawablePadding="10dp"          android:hint="请输入密码"          android:inputType="testPasword"          android:ems="10"          >          </EditText>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_below="@+id/etPassword"        android:layout_alignParentLeft="true"        android:layout_alignParentStart="true">        <CheckBox            android:text="记住密码"            android:layout_width="0dp"            android:layout_height="wrap_content"            android:id="@+id/cbIsRememberPass"            android:layout_weight="1"            android:textSize="20sp"            />        <Button            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="登录"            android:onClick="login"            android:background="@drawable/btn_select"/>    </LinearLayout></RelativeLayout>

3:实现button按钮的点击啥时间onclick函数,并实现其他变量或方法的调用:

package com.example.loginapplication;import android.content.Context;import android.content.SharedPreferences;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.CheckBox;import android.widget.EditText;public class LoginActivity extends AppCompatActivity {    private EditText etName;        private EditText etPassword;        private CheckBox cbIsRememberPass;        private SharedPreferences sharedPreferences;        @Override        protected void onCreate (Bundle savedInstanceState){        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_login);        initViews();        sharedPreferences = getSharedPreferences("rememberpassword", Context.MODE_PRIVATE);    }    public void initViews() {        etName = (EditText) findViewById(R.id.etName);        etPassword = (EditText) findViewById(R.id.etPassword);        cbIsRememberPass = (CheckBox) findViewById(R.id.cbIsRememberPass);    }    public void login(View view) {        String name = etName.getText().toString();        String password = etPassword.getText().toString();        if ("admin".equals(name) && "123456".equals(password)) {            SharedPreferences.Editor editor = sharedPreferences.edit();            if (cbIsRememberPass.isChecked())                editor.putBoolean("rememberpassword", true);            editor.putString("name", name);            editor.putString("password", password);        } else {            editor.clear();        }        editor.commit();    }}

4:添加一个空的Activity命名为MainActivity实现登录成功弹出的欢迎字幕“Welcome you !”

<?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:id="@+id/activity_main"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context="com.example.loginapplication.MainActivity">    <TextView        android:text="Wwlcom you!"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:textSize="20sp"        android:layout_centerVertical="true"        android:layout_centerHorizontal="true"        android:id="@+id/textView" /></RelativeLayout>

5:完善数据输入的正确性:
这里写图片描述

6:到这里,我们的程序就算是做完了。最后进行程序测试:
这里写图片描述

0 0
原创粉丝点击