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、实现成果
相关推荐
大、男人几秒前
python之contextmanager
android·python·adb
不法2 小时前
java查看安卓证书信息
android
儿歌八万首2 小时前
Jetpack Compose 动画实战:让你的 UI 动起来
android·kotlin·动画·compose
千里马学框架3 小时前
如何改进车载三分屏SplitScreen启动交互方式?
android·智能手机·分屏·aaos·安卓framework开发·车载开发·3分屏
REDcker4 小时前
Android WebView 版本升级方案详解
android·音视频·实时音视频·webview·js·编解码
麦兜*4 小时前
【springboot】图文详解Spring Boot自动配置原理:为什么@SpringBootApplication是核心?
android·java·spring boot·spring·spring cloud·tomcat
le1616164 小时前
Android 依赖种类及区别:远程仓库依赖、打包依赖、模块依赖、本地仓库依赖
android
lxysbly4 小时前
psp模拟器安卓版带金手指
android
云上凯歌5 小时前
02 Spring Boot企业级配置详解
android·spring boot·后端
hqiangtai5 小时前
Android 高级专家技术能力图谱
android·职场和发展