欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

一二周学习总结

程序员文章站 2022-03-23 23:14:44
...

Android Studio的下载和安装

下载和安装

Android Support v4\v7\v13和AndroidX的区别及应用场景

区别及应用场景

项目的实践

  • 问候欢迎界面
  • 跳转界面
    点击注册按钮跳转到注册界面
Button tvRegister = findViewById(R.id.register_login);
        tvRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent =new Intent(LoginActivity.this,RegisterActivity.class);
                startActivity(intent);
            }
        });
  • Toolbar顶部导航栏的设置
<include layout="@layout/tool_bar" />

登录界面的顶部导航栏

private void initToolbar(){
        toolbar = findViewById(R.id.title_toolbar);
        toolbar.setTitle("登录");
        setSupportActionBar(toolbar);

        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);//设置返回键
//            actionBar.setHomeButtonEnabled(true);//设置是否是首页
        }
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                LoginActivity.this.finish();
            }
        });
    }
  • 登录注册功能
    验证用户名和密码,正确后跳转到我的界面
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initToolbar();
        initView();
        initData();
        btnLogin.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                String username=etUsername.getText().toString();
                String password=etPassword.getText().toString();
                SharedPreferences pref = getSharedPreferences("data", MODE_PRIVATE);
                String name = pref.getString("username", "");
                String pwd = pref.getString("password", "");
                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 (!username.equals(name)) {
//                    Toast.makeText(LoginActivity.this, "用户名错误"+username+","+name , Toast.LENGTH_SHORT).show();
                } else if (!MD5Utils.md5(password).equals(pwd)) {
                    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("longinUser",username);
                    setResult(RESULT_OK,intent);
                    LoginActivity.this.finish();
                }
            }
        });
    }
  • 底部导航栏
<?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>
  • 登出功能
// 退出登录
       exitLayout.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               Toast.makeText(SettingActivity.this, "退出登录", Toast.LENGTH_SHORT).show();

               new AlertDialog.Builder(SettingActivity.this)
                       .setTitle("退出")
                       .setMessage("确认退出登录?")
                       .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                           @Override
                           public void onClick(DialogInterface dialog, int which) {
                               SharedUtils.clearLoginInfo(SettingActivity.this);

                               // 返回我的界面
                               Intent intent = new Intent();
                               intent.putExtra("isLogin", false);
                               setResult(RESULT_OK, intent);
                               SettingActivity.this.finish();
                           }
                       })
                       .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                           @Override
                           public void onClick(DialogInterface dialog, int which) {
                               dialog.dismiss();
                           }
                       })
                       .show();

           }
       });
相关标签: Android学习