原生Android开发基础项目结构介绍

原生Android开发基础项目结构介绍


    下面先来总结一下软件的基本目录结构,这一般是开发的关键,只有明白了软件的目录结构,才能更加可灵活的设计软件。关注过我的人应该都知道,我主要是走的前端方向,然后摸一摸基础的后端,盘一盘网安,偶尔为自己的软件接口抓个包什么的。因此下面的介绍会通过与前端的对比来总结。

1.软件整体结构

在这里插入图片描述
(1)权限设置:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

最常规的比如上面的网络权限设置和文件读写权限等,具体需要的权限可以根据实际开发需要去查找就行;
(2)软件基本配置:

 <application
        android:allowBackup="true"
        android:icon="@drawable/campus_notice"
        android:label="@string/app_name"
        android:networkSecurityConfig="@xml/network_security_config"
        android:requestLegacyExternalStorage="true"
        android:roundIcon="@drawable/campus_notice"
        android:supportsRtl="true"
        android:theme="@style/Theme.MqttLedMenu">
 </application>

解释:上面的@引入的资源其实就在drawablevalues目录下,
在这里插入图片描述
说明一下network_security_config,因为Android默认只能使用https访问,但是由于项目调试以及部分接口不是安全协议,因此可能需要用到。具体内容如下(通过xml引入),详细了解可以浏览(Android网络安全之NetworkSecurityConfig)

<?xml version="1.0" encoding="utf-8"?>
<network-security-config xmlns:android="http://schemas.android.com/apk/res/android">
    <base-config cleartextTrafficPermitted="true" />
</network-security-config>

2.具体的页面书写

    接下来以MainActivity页面为例,注意这里的名字是默认的,也建议采用这个,类似于微信小程序里面的index,不叫index也可以,只是大家习惯这么命名
在这里插入图片描述
    对于页面的XML文件的功能按钮做一下说明:
在这里插入图片描述

3.实例分析

    以登录页面的为例,补充,写入了LoginActivity页面,要想在页面中具体使用,需要在AndroidManifest.xml中做如下注册:

<activity
   	android:name=".LoginActivity"
    android:exported="true">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
    <!--intent-filter部分其实就表示默认展示页面-->
</activity>

分析如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:background="@drawable/background"
    android:orientation="vertical"
    android:padding="30dp"
    tools:context=".LoginActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="160dp"
        android:orientation="vertical">

        <TextView
            android:layout_width="60dp"
            android:layout_height="wrap_content"
            android:text="用户名:"
            android:textFontWeight="@integer/material_motion_duration_long_2" />

        <EditText
            android:id="@+id/username"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="请输入用户名~"
            android:textFontWeight="@integer/material_motion_duration_medium_1" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="密码:"
            android:textFontWeight="@integer/material_motion_duration_long_2" />

        <EditText
            android:id="@+id/password"
            android:layout_width="823dp"
            android:layout_height="wrap_content"
            android:hint="请输入登录密码~"
            android:password="true"
            <!--开启密码显示-->
            android:textFontWeight="@integer/material_motion_duration_medium_1"
            tools:ignore="Deprecated" />
    </LinearLayout>

    <LinearLayout
    <!--emm,这里的布局被误调了,尴尬,不建议这么书写-->
        android:layout_width="800dp"
        android:layout_height="88dp"
        android:orientation="horizontal">

        <CheckBox
            android:id="@+id/isRemember"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="记住密码">

        </CheckBox>

        <TextView
            android:id="@+id/isForget"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="40dp"
            android:text="忘记密码?" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="30dp">

        <Button
            android:id="@+id/login"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@color/btn"
            android:padding="10dp"
            android:text="登录" />
    </LinearLayout>

</LinearLayout>

对应的逻辑代码如下:

package com.example.mqttledmenu;
//导入部分我给省了,如果报错就直接alt+enter导入就行
public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
    private EditText username;
    private EditText password;
    private CheckBox isRemember;
    private TextView isForget;
    private Button login;
    private SharedPreferences sp;
    //利用SharedPreferences存储用户密码实现自动登录功能
    @Override
    protected void onCreate(Bundle savedInstanceState) {
   		 //初始化UI绑定,相当于html最原始的document的getElementById
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        username = findViewById(R.id.username);
        password = findViewById(R.id.password);
        login = findViewById(R.id.login);
        isRemember = findViewById(R.id.isRemember);
        isForget = findViewById(R.id.isForget);
        //绑定点击事件,这里因为implements(实现)了View.OnClickListener接口,因此
        //可以实现对绑定时间的集体封装,最后利用switch case便于统一管理点击事件。 
        login.setOnClickListener(this);
        isRemember.setOnClickListener(this);
        isForget.setOnClickListener(this);

        //检测密码信息
        sp = getSharedPreferences("config", Context.MODE_PRIVATE);
        Boolean autoLogin = sp.getBoolean("remember", false);
        if (autoLogin) {
            username.setText(sp.getString("username", ""));
            password.setText(sp.getString("password", ""));
            isRemember.setChecked(true);
        } else {
            isRemember.setChecked(false);
        }
    }
	//集中管理点击事件
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.login:
                String usernameStr = username.getText().toString();
                String passwordStr = password.getText().toString();
                if (usernameStr.equals("") || passwordStr.equals("")) {
                    Toast.makeText(this, "用户名和密码不能为空!", Toast.LENGTH_SHORT).show();
                } else {
                //登录请求的逻辑,稍后新开一个文章详细解释,基础不好的先可以跳过
                    Retrofit retrofit = new Retrofit.Builder().baseUrl(Config.serverUrl).build();
                    UploadService uploadService = retrofit.create(UploadService.class);
                    Call<ResponseBody> call = uploadService.login(usernameStr, passwordStr);
                    call.enqueue(new Callback<ResponseBody>() {
                        @Override
                        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                            String result = null;
                            try {
                                result = response.body().string();
                                JsonObject returnData = new JsonParser().parse(result).getAsJsonObject();
                                if (!returnData.get("code").toString().equals("200")) {
                                    Toast.makeText(LoginActivity.this, "用户名或密码错误~", Toast.LENGTH_SHORT).show();
                                    return;
                                }
                                JsonObject temp = new JsonParser().parse(returnData.get("data").toString()).getAsJsonObject();
                                Config.getInstance().setUserId(temp.get("userId").toString());//存储用户的id
                                Config.getInstance().setUserAvatar(temp.get("userAvatar").toString());//存储用户的头像
                                if (isRemember.isChecked()) {
                                    //存储用户用户名和密码
                                    SharedPreferences.Editor edit = sp.edit();
                                    edit.putString("username", usernameStr);
                                    edit.putString("password", passwordStr);
                                    edit.putBoolean("remember", true);
                                    edit.apply();
                                }
								//页面跳转逻辑
                                Intent intent = new Intent();
                                //设置不能返回上一页,其实就是清空(clear)了页面栈
                                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                                //第一个参数:当前页面(getApplicationContext能够自动获取当前页面)
                                //第二个参数:想要跳转的页面
                                intent.setClass(getApplicationContext(), MainActivity.class);
                                startActivity(intent);
                            } catch (IOException e) {
                                Toast.makeText(LoginActivity.this, "服务端异常~", Toast.LENGTH_SHORT).show();
                                e.printStackTrace();
                            }
                        }

                        @Override
                        public void onFailure(Call<ResponseBody> call, Throwable t) {

                        }
                    });
                }
                break;
            case R.id.isForget:
                Toast.makeText(this, "请联系辅导员重置密码哦!", Toast.LENGTH_SHORT).show();
                break;
        }
    }
}

4.后续

    由于时间比较紧,可能写的相对粗糙,如果有什么不懂的可以在评论区提出来,我会根据时间尽快安排新的文章,喜欢的话可以点个赞支持一下呀!。

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
为了满足广大Android开发爱好者与从业者的学习需求,我们精心整理并上传了一份全面而实用的Android项目资源包。这份资源包内容丰富,涵盖了从基础知识到实战应用的全方位内容,旨在为开发者们提供一个便捷、高效的学习平台。 一、文件手册 资源包中的文件手册部分,详细记录了Android开发的核心知识点和常用技术。无论是初学者还是有一定经验的开发者,都能从中找到所需的学习资料。手册采用了简洁明了的排版方式,使得查阅更加方便快捷。同时,手册内容深入浅出,既适合新手入门,也能为老手提供有价值的参考。 二、项目实战与练习 为了让学习者能够将理论知识与实践相结合,我们特别准备了项目实战与练习部分。这部分内容包含了多个精心设计的Android项目案例,从需求分析、设计思路到实现过程,都有详细的讲解和代码示例。学习者可以通过实际操作,深入了解Android开发的整个流程,提升自己的实战能力。 此外,我们还提供了一系列练习题,旨在巩固所学知识,检验学习成果。这些练习题既有基础题,也有难度较高的挑战题,适合不同层次的学习者进行练习。 三、Android开发工具集 在Android开发过程中,选择合适的工具能够大大提高开发效率。因此,我们整理了常用的Android开发工具集,包括开发工具、测试工具、性能优化工具等。这些工具都是经过我们精心筛选和测试的,能够帮助开发者们更加高效地进行Android开发工作。 总的来说,这份Android项目资源包是一份不可多得的学习资料,无论你是初学者还是有一定经验的开发者,都能从中受益匪浅。我们希望通过这份资源包,为广大Android开发爱好者与从业者提供一个更加便捷、高效的学习平台,共同推动Android开发领域的发展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

凌空暗羽

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值