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、实现成果
相关推荐
HLC++10 小时前
Linux的进程间通信
android·linux·服务器
爱笑鱼14 小时前
Binder(二):AIDL 生成的 Proxy、Stub 和 Parcel 到底在做什么?
android
爱笑鱼14 小时前
Binder(一):一次方法调用,究竟怎样跨进程执行?
android
壮哥_icon15 小时前
【Android 系统开发】使用 BAT 脚本高效自动化管理 /system/priv-app/ 系统应用(安装与卸载)
android·运维·自动化
用户693717500138416 小时前
Claude Code终端日志Token占用实测:一个过滤器砍掉60%-90%
android·前端·后端
蝉蜕日记17 小时前
AccumuPDF高级版 v2.57 | 视图文转换PDF,合并分割压缩
android·智能手机·pdf·生活·软件需求
吐了啊取名字太难17 小时前
美颜系统AI修图本地跑并支持Mac、win、安卓、iOS不卡顿
android·人工智能·windows·数码相机·mac·ai编程
阿pin18 小时前
Android随笔-Retrofit
android·retrofit
想取一个与众不同的名字好难18 小时前
安卓自定义颜色选择器
android
我命由我1234519 小时前
Android 开发问题:java.lang.NoSuchMethodError:No virtual method readAllBytes()
android·java·java-ee·kotlin·android studio·android-studio·android runtime