androidStudio设计一个登录页面,并且能记住账号和密码的功能

用androidStudio设计一个登录页面,并且能记住账号和密码的功能!

目前大多数市场的APP都会存在一个登录界面设计,一个好的登录界面如果功能强大,是很吸引用户的,也极大的给用户带来舒适的体验,那么一个好的登录界面除了界面的美观、还应该具有操作简单、能够记住密码等,本次博客,我们将会给大家讲解如何让登录界面记住密码!

(一)、登录界面的设计

这里学长采用的登录界面的设计是在线性布局的基础上结合框架布局形成的一个现代化风格的界面,首先我们来看下设计的效果图!
在这里插入图片描述
可以看到,以上具有的功能包括记住密码、自动登录、忘记密码的处理方法、以及注册账号,但学长目前完成的只有记住密码模块,其他的以后再慢慢进行讲解!
1、界面设计的xml代码如下所示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent"
    android:background="@drawable/back6"
    android:id="@+id/back3"
    tools:context=".MainActivity">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="235dp">
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/back"
            android:src="@drawable/pto4" />
        <ImageView
            android:layout_width="130dp"
            android:layout_height="130dp"
            android:id="@+id/pto"
            android:layout_marginTop="70dp"
            android:layout_marginLeft="5dp"
            android:src="@drawable/pto"/>
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/userName"
            android:layout_marginTop="70dp"
            android:hint="@string/user"
            android:layout_marginLeft="5dp"
            android:inputType="text"
            android:layout_toRightOf="@id/pto"/>
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/password"
            android:hint="@string/passward"
            android:inputType="textPassword"
            android:layout_marginTop="120dp"
            android:layout_marginLeft="5dp"
            android:layout_toRightOf="@id/pto"/>
        <CheckBox
            android:layout_width="90dp"
            android:layout_height="wrap_content"
            android:id="@+id/remember"
            android:layout_toRightOf="@id/pto"
            android:layout_marginLeft="5dp"
            android:text="记住密码"
            android:layout_below="@+id/password"/>
        <CheckBox
            android:layout_width="90dp"
            android:layout_height="wrap_content"
            android:layout_below="@+id/password"
            android:id="@+id/right1"
            android:layout_toRightOf="@+id/remember"
            android:text="自动登录"
            />
    </RelativeLayout>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="登录"
        android:id="@+id/login"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:layout_width="match_parent"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:textColor="#FF4081"
            android:text="忘记密码?"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:textColor="#FF4081"
            android:gravity="right"
            android:text="注册账号"/>
    </LinearLayout>
</LinearLayout>

以上为界面设计的整体代码,大家可以结合参考一下,这里不进行xml代码内容的讲解,相信大家也能够理解啦!

(二)、完成记住账号和密码的功能

完成记住账号和密码的功能首先我们得知道账号和密码如果需要保存,那我们得有一个存储空间,android给我们提供了一个保存key-value键值对存储数据的方式:利用SharedPreferences存储数据
1、利用SharedPreferences存储账号和密码
SharedPreferences的本质是基于XML文件存储key-value键值对数据,通常用来存储一些简单的配置信息,用Sqlite数据库来存放并不划算,因为数据库连接跟操作等耗时大大影响了程序的效率。其存储位置在/data/data/<包名>/shared_prefs目录下。
另外SharedPreferences只能保存简单类型的数据,例如,String、int等。一般会将复杂类型的数据转换成Base64编码,然后将转换后的数据以字符串的形式保存在 XML文件中,再用SharedPreferences保存。
这类配置信息是以“键值对”的形式存在的。比如配置语言信息
{“language”,“中文”}
接下来,让我们结合代码进行实例讲解:
第一步:我们声明一个SharedPreferences对象:

private SharedPreferences loginPreference;

第二步:然后在activity的创建方法中,实例化:

loginPreference = getSharedPreferences("login", MODE_PRIVATE);

第三步:使用SharedPreferences接口的edit获得SharedPreferences.Editor对象。并通过对象实现键值的输入,并提交:

SharedPreferences.Editor editor = loginPreference.edit();

第四步:读出配置:

editor.putBoolean("checked", remember.isChecked());
        if (checked) {
            editor.putString("userName", userName1.getText().toString());
            editor.putString("password", password1.getText().toString());
        } else {
            editor.remove("userName").remove("password");
        }
        editor.commit();

分解开来就是以上四步,现在我们进行整体的讲解;
2、完成以上界面的账号和密码保存:
1、我们声明一个SharedPreferences对象和checkBoxs控件对象已经账号和密码:

private SharedPreferences loginPreference;
private EditText userName1;
private EditText password1;
private CheckBox remember;
private Button login;

2、将以上的对象进行实例化操作:

userName1 = (EditText) findViewById(R.id.userName);
password1 = (EditText) findViewById(R.id.password);
remember = (CheckBox) findViewById(R.id.remember);
login = (Button) findViewById(R.id.login);
loginPreference = getSharedPreferences("login", MODE_PRIVATE);
///要通过loginPreference去记录三个参数(checked,userName,password)
boolean cheched = loginPreference.getBoolean("checked", false);
        if (cheched) {
            Map<String, Object> m = readLogin();
            if (m != null) {
                userName1.setText((CharSequence) m.get("userName"));
                password1.setText((CharSequence) m.get("password"));
                remember.setChecked(cheched);
            }
        }

3、登录的事件绑定:

 login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                configLoginInfo(remember.isChecked());
                login();
            }
        });

4、密码保存函数的代码编写:

public void configLoginInfo(boolean checked) {
        SharedPreferences.Editor editor = loginPreference.edit();
        editor.putBoolean("checked", remember.isChecked());
        if (checked) {
            editor.putString("userName", userName1.getText().toString());
            editor.putString("password", password1.getText().toString());
        } else {
            editor.remove("userName").remove("password");
        }
        editor.commit();
    }

5、再次进入时进行账号和密码的读取:

/**
     * 读登录信息
     *
     * @return
     */
    public Map<String, Object> readLogin() {
        Map<String, Object> m = new HashMap<>();
        String userName = loginPreference.getString("userName", "");
        String password = loginPreference.getString("password", "");
        m.put("userName", userName);
        m.put("password", password);
        return m;
    }

到这里,我们就可以实现以上界面设计的账号和密码的保存了!
以上,就是本次博客的全部内容啦,感谢大家收看,记得点赞评论哦!

  • 24
    点赞
  • 167
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 23
    评论
好的,我了解了,让我来为您解答。 首先,您需要创建一个新的项目并在 Android Studio 中打开。接下来,您需要创建登录界面的布局,包括单选按钮、文本框、复选框等控件。 以下是一个基本的登录界面布局示例: ```xml <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical" android:padding="16dp"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Login" android:textSize="24sp" android:gravity="center"/> <RadioGroup android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:paddingTop="16dp" android:paddingBottom="16dp"> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="账号密码登录" /> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="验证码登录" /> </RadioGroup> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="输入账号(11位)" android:maxLines="1" android:inputType="number" android:maxLength="11"/> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="输入密码(6位)" android:maxLines="1" android:inputType="textPassword" android:maxLength="6"/> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="输入验证码" android:maxLines="1"/> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="记住密码"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:orientation="horizontal"> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="显示密码"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="登录"/> </LinearLayout> </LinearLayout> ``` 以上是登录界面的基本布局,其中包含账号密码登录、验证码登录、输入账号密码、输入验证码、记住密码、显示密码登录等控件。 接下来,在 Java 中编写代码处理登录逻辑,包括单选按钮的选择、文本框输入数据的验证、记住密码的勾选、显示密码的切换等。以下是一个大致的登录逻辑: ```java public class LoginActivity extends AppCompatActivity { private RadioGroup mRadioGroup; private RadioButton mRadioButtonAccount, mRadioButtonCode; private EditText mEditTextAccount, mEditTextPassword, mEditTextCode; private CheckBox mCheckBoxRemember, mCheckBoxShow; private Button mButtonLogin; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); mRadioGroup = findViewById(R.id.radio_group); mRadioButtonAccount = findViewById(R.id.radio_button_account); mRadioButtonCode = findViewById(R.id.radio_button_code); mEditTextAccount = findViewById(R.id.edit_text_account); mEditTextPassword = findViewById(R.id.edit_text_password); mEditTextCode = findViewById(R.id.edit_text_code); mCheckBoxRemember = findViewById(R.id.check_box_remember); mCheckBoxShow = findViewById(R.id.check_box_show); mButtonLogin = findViewById(R.id.button_login); mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { switch (checkedId) { case R.id.radio_button_account: mEditTextCode.setVisibility(View.GONE); mEditTextPassword.setVisibility(View.VISIBLE); break; case R.id.radio_button_code: mEditTextPassword.setVisibility(View.GONE); mEditTextCode.setVisibility(View.VISIBLE); break; } } }); mButtonLogin.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String account = mEditTextAccount.getText().toString(); String password = mEditTextPassword.getText().toString(); String code = mEditTextCode.getText().toString(); if (TextUtils.isEmpty(account) || TextUtils.isEmpty(password) || TextUtils.isEmpty(code)) { Toast.makeText(getApplicationContext(), "输入不能为空", Toast.LENGTH_SHORT).show(); return; } if (validated(account, password)) { if (mCheckBoxRemember.isChecked()) { // 记住密码 } // 跳转到主界面或者其他界面 } else { Toast.makeText(getApplicationContext(), "用户名或密码错误", Toast.LENGTH_SHORT).show(); } } }); mCheckBoxShow.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (isChecked) { mEditTextPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance()); } else { mEditTextPassword.setTransformationMethod(PasswordTransformationMethod.getInstance()); } } }); } private boolean validated(String account, String password) { if (account.length() == 11 && password.length() == 6) { // 数据库验证逻辑 return true; } return false; } } ``` 以上是基本的登录逻辑,其中包括单选按钮的选中处理、输入数据的验证、密码的显示与隐藏等常见功能。 最后,您需要在 AndroidManifest.xml 中添加登录界面的启动器,以便在应用程序启动时进入登录界面。 ```xml <activity android:name=".LoginActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> ``` 以上就是基于 Android Studio登录界面的实现方法。我希望这可以帮助到您。
评论 23
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陈一月的编程岁月

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值