安卓第一周第二周知识点整理

2019-09-30  本文已影响0人  国王兔子

1.Android studio安装与配置

2.基本页面

登录页面基本控件

3.页面之间的跳转

  TextView tvRegister = findViewById(R.id.tv_register);
        tvRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivityForResult(intent, 1);
            }
        });

        Button btnLogin = findViewById(R.id.btn_login);
        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                login();

            }
        });
    }

4.登录注册

 private void register() {
        // 3.1
        String username = etUsername.getText().toString();
        String password = etPassword.getText().toString();
        String pwdAgain = etPwdAgain.getText().toString();
        // 3.2
        if (TextUtils.isEmpty(username)) {
            Toast.makeText(RegisterActivity.this, "用户名不能为空",
                    Toast.LENGTH_SHORT).show();
        } else if (TextUtils.isEmpty(password) || TextUtils.isEmpty(pwdAgain)) {
            Toast.makeText(RegisterActivity.this, "密码不能为空",
                    Toast.LENGTH_SHORT).show();
        } else if (!password.equals(pwdAgain)) {
            Toast.makeText(RegisterActivity.this, "两次密码必须一致",
                    Toast.LENGTH_SHORT).show();
        } else if (isExist(username)) {
            Toast.makeText(RegisterActivity.this, "此用户已存在",
                    Toast.LENGTH_SHORT).show();
        } else {
            // 注册成功之后
            savePref(username, MD5Utils.md5(password));
            Intent intent = new Intent();
            intent.putExtra("username", username);
            setResult(RESULT_OK, intent);
            finish();
        }
    }

private void login() {
        String username = etUsername.getText().toString();
        String password = etPassword.getText().toString();
        password = MD5Utils.md5(password);
        String spPwd = readPwd(username);

        if (TextUtils.isEmpty(username)) {
            Toast.makeText(LoginActivity.this, "用户名不能为空", Toast.LENGTH_SHORT).show();
        } else if (TextUtils.isEmpty(password)) {
            Toast.makeText(LoginActivity.this, "密码不能为空", Toast.LENGTH_SHORT).show();
        } else if (TextUtils.isEmpty(spPwd)) {
            Toast.makeText(LoginActivity.this, "请先注册", Toast.LENGTH_SHORT).show();
        } else if (!spPwd.equals(password)) {
            Toast.makeText(LoginActivity.this, "输入的密码不正确", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
            saveLoginStatus(username, true);

            // 返回到我的界面
            Intent intent = new Intent();
            intent.putExtra("isLogin", true);
            intent.putExtra("loginUser", username);
            setResult(RESULT_OK, intent);
            LoginActivity.this.finish();
        }
    }

5.导航栏

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <LinearLayout
        android:id="@+id/main_body"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/white"
        android:orientation="vertical">

    </LinearLayout>

    <RadioGroup
        android:id="@+id/btn_group"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:layout_alignParentBottom="true"
        android:background="#F2F2F2"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/btn_course"
            style="@style/TabMenuItem"
            android:drawableTop="@drawable/selector_nav_course"
            android:text="课程" />

        <RadioButton
            android:id="@+id/btn_execise"
            style="@style/TabMenuItem"
            android:drawableTop="@drawable/selector_nav_execise"
            android:text="习题" />

        <RadioButton
            android:id="@+id/btn_message"
            style="@style/TabMenuItem"
            android:drawableTop="@drawable/selector_nav_message"
            android:text="资讯" />

        <RadioButton
            android:id="@+id/btn_my"
            style="@style/TabMenuItem"
            android:drawableTop="@drawable/selector_nav_my"
            android:checked="true"
            android:text="我" />
    </RadioGroup>

</RelativeLayout>

6.顶部导航栏

<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/title_toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"
    android:clipToPadding="true"
    android:fitsSystemWindows="true"
    android:minHeight="?attr/actionBarSize"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    app:titleTextColor="@android:color/white">

</androidx.appcompat.widget.Toolbar>
上一篇下一篇

猜你喜欢

热点阅读