android 登录界面编写

1、登录页面实现内容

1.实现使用两个EditText输入框输入用户名和密码。

2.使用CheckBox控件记住密码功能。

3.登录时候,验证用户名和密码是否为空。

4.当前CheckBox控件记住密码勾上时,使用SharedPreferences存储用户名和密码。

5.登录时候使用ProgressDialog登录转圈圈2秒,两秒后显示登录成功。

6.默认用户名和密码admin和admin。当用户名和密码输入都是admin就提示登录成功。

2、登录页面布局实现
XML 复制代码
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/tvTitle"
        style="@style/TextView"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:layout_marginTop="20dp"
        android:gravity="center"
        android:text="登录"
        android:textSize="24sp"
        android:textStyle="bold"
        app:layout_constraintTop_toTopOf="parent" />

    <LinearLayout
        android:id="@+id/layoutInput"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="30dp"
        android:gravity="center"
        android:orientation="vertical"
        app:layout_constraintTop_toBottomOf="@id/tvTitle">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center">

            <TextView
                style="@style/TextView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="@dimen/layout_left_distance"
                android:text="用户名:" />

            <EditText
                android:id="@+id/editUser"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginRight="@dimen/layout_right_diatance"
                android:ems="10" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center">

            <TextView
                style="@style/TextView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="@dimen/layout_left_distance"
                android:text="密   码:" />

            <EditText
                android:id="@+id/editPsw"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginRight="@dimen/layout_right_diatance"
                android:ems="10"
                android:inputType="textPassword" />
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/layoutCheck"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toBottomOf="@id/layoutInput"
        android:gravity="center">

        <CheckBox
            android:id="@+id/checkBoxRemember"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="80dp"
            android:text="记住密码" />
    </LinearLayout>

    <Button
        android:id="@+id/buttonLogin"
        style="@style/button"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginLeft="50dp"
        android:layout_marginTop="20dp"
        android:layout_marginRight="50dp"
        android:text="登录"
        app:layout_constraintTop_toBottomOf="@id/layoutCheck"
        tools:ignore="MissingConstraints" />
</androidx.constraintlayout.widget.ConstraintLayout>
3、fragment实现登录界面
XML 复制代码
public class LoginFragment extends Fragment {
    private ProgressDialog progressDialog = null;

    private View rootView;
    private EditText editUser, editPsw;
    private CheckBox checkBoxRemember;
    public LoginFragment() {
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_loglin, container, false);
        editUser = rootView.findViewById(R.id.editUser);
        editPsw = rootView.findViewById(R.id.editPsw);
        checkBoxRemember = rootView.findViewById(R.id.checkBoxRemember);

        SharedPreferences sp = getActivity().getSharedPreferences("mmsx", MODE_PRIVATE);

        editUser.setText(sp.getString("user",""));
        editPsw.setText(sp.getString("password",""));
        checkBoxRemember.setChecked(sp.getBoolean("remember",true));

        View buttonLogin =  rootView.findViewById(R.id.buttonLogin);
        buttonLogin.setOnClickListener(view -> {
            String user = editUser.getText().toString();
            String psw = editPsw.getText().toString();
            if (user.isEmpty()){
                Toast.makeText(getActivity(),"请输入用户名", Toast.LENGTH_LONG).show();
                return;
            }
            if (psw.isEmpty()){
                Toast.makeText(getActivity(),"请输入密码", Toast.LENGTH_LONG).show();
                return;
            }
            if (user.equalsIgnoreCase("admin") && psw.equalsIgnoreCase("admin")){
                SharedPreferences.Editor edit = Objects.requireNonNull(getActivity()).getSharedPreferences("mmsx", MODE_PRIVATE).edit();
                if (checkBoxRemember.isChecked()){
                    edit.putString("user", "admin");
                    edit.putString("password", "admin");
                }else {
                    edit.putString("user", "");
                    edit.putString("password", "");
                }
                edit.putBoolean("remember", checkBoxRemember.isChecked());
                edit.apply();

                progressDialog=new ProgressDialog(getActivity());
                progressDialog.setTitle("登录中");
                progressDialog.setMessage("登录中,请稍后...");
                progressDialog.setCancelable(true);
                progressDialog.show();
                //这里的话新建一个线程,重写run()方法,
                new Thread()
                {
                    public void run()
                    {
                        SystemClock.sleep(2000);//把信息码发送给handle让更新界面
                        handler.sendEmptyMessage(123);
                    }

                }.start();
            }else {
                Toast.makeText(getActivity(),"用户名或者密码错误", Toast.LENGTH_LONG).show();
            }
        });

        return rootView;
    }

    @SuppressLint("HandlerLeak")
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 123) {
                progressDialog.dismiss();
                Toast.makeText(getActivity(),"登录成功", Toast.LENGTH_LONG).show();
            }
        }
    };
}
4、实现成果
相关推荐
千里马学框架3 天前
一起学 Android 14:ShellTransition 屏幕旋转过程深度剖析
android·智能手机·性能优化·framework·性能·屏幕旋转·rotation
美狐美颜SDK开放平台3 天前
开发直播APP时如何接入视频美颜SDK?开发流程与注意事项
android·人工智能·计算机视觉·音视频·直播美颜sdk
AFinalStone3 天前
Android7 SystemUI源码解析(七)Keyguard锁屏模块深度解析
android·systemui
致远ccc3 天前
Google Play 上架前如何测试 App?多国家 Android 环境测试
android·app测试·googleplay·多国家应用测试
ttyyttemo3 天前
Kotlin 协程中的 Job 结构化并发与取消
android
sun0077003 天前
tbox 4g/5g切换,导致wan ip 改变,导致车机旧网络不可用。需要重启车机才行
android
其实防守也摸鱼3 天前
内网穿透与反向代理:原理、工具与实战指南
android·大数据·运维·安全·网络安全·自动化·渗透
AFinalStone3 天前
Android7 SystemUI 源码解析(四)NavigationBar 导航栏与 SystemBars
android·systemui
JMchen3 天前
属性动画原理与高级动画实现
android·kotlin·canvas
AFinalStone3 天前
Android7 SystemUI 源码解析(二)启动流程深度解析
android·systemui