记住密码的用户登录

一、运行效果图:
这里写图片描述
这里写图片描述
二、实现过程:
(1)、布局文件:
这里写图片描述
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/activity_main"
    android:background="@drawable/loginbg"
    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="bzu.edu.cn.case_login.MainActivity">
    <include layout="@layout/login_top">
    </include>
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@drawable/deer"
        android:id="@+id/imageView"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />
</RelativeLayout>

login_top.xm

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/logintop_groungdg"
    android:padding="@dimen/activity_horizontal_margin">
    <EditText
        android:id="@+id/etName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:background="@android:drawable/edit_text"
        android:drawableLeft="@drawable/icon_user"
        android:hint="@string/etName"
        android:drawablePadding="10dp"
        >
        <requestFocus/>
    </EditText>
    <EditText
        android:id="@+id/etPassword"
        android:layout_below="@id/etName"
        android:inputType="textPassword"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:background="@android:drawable/edit_text"
        android:drawableLeft="@drawable/icon_pass"
        android:hint="@string/etName"
        android:drawablePadding="10dp">
        <requestFocus/>
    </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:layout_width="0dp"
            android:layout_height="48dp"
            android:layout_weight="1"
            android:id="@+id/cblsRemember"
            android:textSize="20sp"
            android:text="记住密码"/>
        <Button
            android:onClick="login"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_marginLeft="10dp"
            android:background="@drawable/btn_select"
            android:text="@string/btnLogin"
            android:textSize="20sp"/>
    </LinearLayout>
</RelativeLayout>

(2)、
MainActivity.xml

package bzu.edu.cn.case_login;

import android.content.Context;
import android.content.Intent;
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;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private EditText etName;
    private EditText etPassword;
    private SharedPreferences sharedPreferences;
    private CheckBox cblsRemember;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        sharedPreferences = getSharedPreferences("rememberpassword", Context.MODE_PRIVATE);
        boolean isRemembered=sharedPreferences.getBoolean("rememberpassword",false);
        if (isRemembered){
            String name=sharedPreferences.getString("name","");
            String password=sharedPreferences.getString("password","");
            etName.setText(name);
            etPassword.setText(password);
            cblsRemember.setChecked(true);
        }
    }
    private void initView() {
        etName = (EditText) findViewById(R.id.etName);
        etPassword = (EditText) findViewById(R.id.etPassword);
        cblsRemember = (CheckBox) findViewById(R.id.cblsRemember);
    }

    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 (cblsRemember.isChecked()) {
                editor.putBoolean("rememberpassword", true);
                editor.putString("name", name);
                editor.putString("password", password);
            }else{
                editor.clear();
            }
            editor.commit();
            Intent intent=new Intent(this,WelcomeActivity.class);
            startActivity(intent);
            finish();
        }else {
            Toast.makeText(this,"账号或密码有误",Toast.LENGTH_LONG).show();
        }
    }
}

WelcomeActivity.xml

package bzu.edu.cn.case_login;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class WelcomeActivity extends AppCompatActivity {
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);
    }
}

3、清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="bzu.edu.cn.case_login">
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".WelcomeActivity"></activity>
    </application>
</manifest>

保存用户名和密码使用了SharedPreferences,它是一种轻型的数据存储方式,它的本质是基于XML文件存储key-value键值对数据,通常用来存储一些简单的配置信息。其存储位置在/data/data/<包名>/shared_prefs目录下。SharedPreferences对象本身只能获取数据而不支持存储和修改,存储修改是通过Editor对象实现。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
WPF 用户登录界面通常由以下几个元素组成: 1. 用户名输入框 2. 密码输入框 3. 登录按钮 4. 记住密码复选框 5. 用户选择下拉框 其中,记住密码复选框用于保存用户的登录信息,方便用户下次登录时直接使用已经保存的信息,而用户选择下拉框用于选择不同的用户账号,如果该应用程序支持多用户登录的话。 以下是一个简单的WPF用户登录界面的示例代码: ```xaml <Window x:Class="WpfApp1.LoginWindow" ... Title="Login" Height="250" Width="350"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto"/> <ColumnDefinition Width="*"/> </Grid.ColumnDefinitions> <Label Grid.Row="0" Grid.Column="0" Margin="5" Content="Username:"/> <TextBox Grid.Row="0" Grid.Column="1" Margin="5" Name="TxtUsername"/> <Label Grid.Row="1" Grid.Column="0" Margin="5" Content="Password:"/> <PasswordBox Grid.Row="1" Grid.Column="1" Margin="5" Name="TxtPassword"/> <CheckBox Grid.Row="2" Grid.Column="1" Margin="5" Name="ChkRememberMe" Content="Remember me"/> <Label Grid.Row="3" Grid.Column="0" Margin="5" Content="User type:"/> <ComboBox Grid.Row="3" Grid.Column="1" Margin="5" Name="CmbUserType"> <ComboBoxItem Content="Admin"/> <ComboBoxItem Content="User"/> </ComboBox> <Button Grid.Row="4" Grid.Column="1" Margin="5" Content="Login" Click="BtnLogin_Click"/> </Grid> </Window> ``` 在代码中,我们通过 Grid 控件来布局界面中的各个元素,其中第一列为固定的标签,第二列为输入控件。 在登录按钮的 Click 事件中,我们可以获取用户输入的用户名、密码记住密码选项和用户类型,然后根据这些信息进行登录验证和授权操作。如果用户勾选了记住密码选项,我们可以将用户名和密码保存到本地,以便下次登录时自动填充。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值