代码编织梦想

前言

最近计划做一个安卓项目,决定系统学习一下安卓,于是跟着b站一个视频学习,把整理出来的笔记分享一下。

视频连接:https://www.bilibili.com/video/BV19U4y1R7zV?p=1&vd_source=edd880713648cfdba73e0de269c4035d

1.安装Andriod Studio和SDK

Andriod Studio:Android Studio 是谷歌推出的一个Android集成开发工具,基于IntelliJ IDEA. 类似 Eclipse ADT,Android Studio 提供了集成的 Android 开发工具用于开发和调试。

SDK:全程Software Development Kit,意即软件开发工具包,可以将App源码编译为可执行应用。

2.Hello World!

1.创建工程与模拟器

创建一个新项目,选择Minimum SDK(最低兼容版本),如果选择5.0,则5.0以上的版本都可以兼容。

6cc19d1dd3bbcbce99833b1a387b08f5.png

创建后自动打开一个xml和java文件

eeca45a32203e142123f21412649e282.png

Create device可以创建安卓虚拟机,让应用运行在安卓设备上。

d7b4319e07634ce145bed667a09d6e81.png

点击"run"即可在安卓虚拟上安装并运行你的APP

30214b7504c93a7f5e9f4d9d9635ecbd.png

2.运行日志、运行环境

0016f50c083518331f4325848b894039.png

App可以在模拟器上运行,也可以在真机上运行

真机运行需要用数据线将手机连接在电脑,打开开发者模式

3.工程结构

App工程分为两个层次,第一个层次是项目(Project),另一个层次是模块(Module)

模块依赖于项目,每个项目至少拥有一个模块,也能拥有多个模块。

一般编译运行app指的是运行某个模块,而非运行某个项目,因为模块才对应实际的app。

e4f0a6ea0d5b7d549b037dd822edcb45.png

从上图看到,该项目下面有两个分类:一个是app(代表app模块);另一个是Gradle。其 中,app下的src有3个子目录,其功能说明如下:

main中的java存放源码,res存放资源,xml是app的运行配置文件。

androidTest和test是测试用的java代码

Grade是工程的编译配置文件

app文件夹

4827632fda31516ab00f257ecd996fda.png

res文件夹下有多个文件夹

d4e73195d3dde18df3f2463e653fb911.png

drawable目录存放图形描述文件与图片文件。

layout目录存放App页面的布局文件。

mipmap目录存放App的启动图标。

values目录存放一些常量定义文件,例如字符串常量strings.xml、像素常量dimens.xml、颜色常 量colors.xml、样式风格定义styles.xml等。

ccb3aa75479161451df465389ebc988c.png

清单文件

bba7123b7012965842b4e3fb6566b5b6.png
266bb3cf89f11c5e234065df228f871a.png

activity是一个应用程序组件,提供一个屏幕,用户可以用来完成交互

cc43740154ceff838438c380d8588416.png

Gradle文件夹

Gradle下面主要是工程的编译配置文件,主要有:

(1)build.gradle,该文件分为项目级与模块级两种,用于描述App工程的编译规则。

(2)proguard-rules.pro,该文件用于描述Java代码的混淆规则。

(3)gradle.properties,该文件用于配置编译工程的命令行参数,一般无须改动。

(4)settings.gradle,该文件配置了需要编译哪些模块。初始内容为include ':app',表示只编译app模 块。

(5)local.properties,项目的本地配置文件,它在工程编译时自动生成,用于描述开发者电脑的环境 配置,包括SDK的本地路径、NDK的本地路径等。

项目级别的build.gradle

5970d94025249121c73a77d9d02cc55e.png
d0d7f2653f4ef25a319c14fc43c7462d.png

4.设计规范

用XML标记描绘应用界面,Java代码书写程序逻辑。好处:提升代码复用性。

5.Activity的创建与跳转

e09fd01a82efcded127b233aaadea2b5.png

1.创建xml

9fb51f6f44278565166633ec6d33f73c.png

2.创建java代码

606be7240918400d65e3d5a056b031e1.png

继承类,重写OnCreate,指定布局

2a308859e8a555a1d65a610aec37c4fa.png

3.在清单文件中注册

5be01c1be6e598ee872065f7ee245ac9.png

4.布局中添加一个按钮

5c555c2249299051ad8d5e514ceb5999.png

5.给按钮点击事件

匿名内部类

idea如何实现 未实现的方法快捷键,鼠标放在类的名称上,按住Alt+Enter键

430e990e1ee0a286005d659740f4181c.png

6创建Activity的简单方式

直接在java文件夹下创建New-Activity-Empty Activity

81f396e10273398ff703098e0848cb87.png

命名

b6be4e38ef55dbe24dbbcbddfac800a6.png

会自动创建java文件和xml

91e2f3a417929226bd71fefac5ff8828.png

7.上面遇到的bug

f290eb1def5113ce8d98f9f233e755c8.png

如果没有将某activity设定为启动activity而直接run,会报错

f4e220e5c0b72ca5dcf49a1effb74673.png

解决:可在AndroidManifest.xml中Activity配置上设置 android:exported="true",或者设置intent-filter

如下:

    <activity android:name=".activity.LoginActivity" >       
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

3.简单控件

1.文本显示

设置文本内容

1.XML文件中通过属性android:text设置文本

2.在Java代码中调用文本视图对象的setText方法设置文本

string中能够有文本

设置文本大小

1.在Java代码中调用setTextSize方法

2.在XML文件中则通过属性android:textSize指定文本大小

需要了解单位

2be82bf54613acb35e964d4fbc2fa248.png

设置文本颜色

1.在Java代码中调用setTextColor方法设置文本颜色,具体色值可以从Color类获取

也可以用8位或者6位16进制数表示,8位前两位表示透明度,ff为完全不透明,00位完全透明,后六位的每两位代表RGB的色值,6位表示的透明度默认为00,即是透明的

2.XML文件textColor

xml中的6位颜色默认是不透明的

3.颜色可以引用在res中定义的颜色,如在colors中定义一个绿色

代码

public class TextViewActivity extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_text);

        TextView tv_hello=findViewById(R.id.tv_hello);
        TextView tv_background=findViewById(R.id.tv_background);
        tv_hello.setText(R.string.hello);
        tv_hello.setTextSize(50);
        tv_hello.setTextColor(Color.GREEN); 		              			//tv_hello.setTextColor(0xff00ff00);
 
        tv_background.setBackgroundColor(Color.BLUE);
    }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <TextView
        android:id="@+id/tv_hello"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello"
        android:textSize="30dp"
        android:textColor="#00ff00"/>
    <TextView
        android:id="@+id/tv_background"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello"
        android:textSize="30dp"
        android:textColor="@color/green"/>
    
</LinearLayout>

2.视图基础

设置视图宽高

➢1.视图宽度通过属性android:layout _width表达,视图高度通过属性android:layout height表达,宽高的取值主要有下列三种:

●match_ parent: 表示与上级视图保持一致。

●wrap_ content: 表示与内容自适应。

●以dp为单位的具体尺寸。

➢2.首先确保XML中的宽高属性值为wrap_ content, 接着打开该页面对应的Java代码,依序执行以下三个步骤:

●调用控件对象的getLayoutParams方法,获取该控件的布局参数。

●布局参数的width属性表示宽度,height属性表示高度,修改这两个属性值。

●调用控件对象的setLayoutParams方法,填入修改后的布局参数使之生效。

package com.example.helloworld;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.helloworld.util.Utils;

public class MainActivity_border extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_border);
        TextView tv_code=findViewById(R.id.tv_code);
        //获取tv_code的布局参数
        ViewGroup.LayoutParams params=tv_code.getLayoutParams();
        params.width= Utils.dip2x(this,300);//设置tv_code的宽
        tv_code.setLayoutParams(params);
    }
}

视图间距

➢设置视图的间距有两种方式:

●采用layout_ margin属性,它指定了当前视图与周围平级视图之间的距离。包括layout_ margin、layout_ marginLeft、 layout marginTop、layout. marginRight、layout marginBottom

●采用padding属性,它指定了当前视图与内部下级视图之间的距离。包括padding、 paddingLef采用填充属性,它指定了当前视图与内部下级视图之间的距离.包括填充、填充Left、paddingTop、 paddingRight、 paddingBottom

d31321e5482cd2dfa6ee0198adfe0b84.png

对齐方式

5164d79483d017592fe153b3deef3f01.png

3.常用布局

线性布局LinearLayout

线性布局内部的各视图有两种排列方式

orientation属性值为horizontal时,内部视图在水平方向从左往右排列

orientation属性值为vertical时,内部视图在垂直方向从上往下排列

不指定属性时默认水平

线性布局的权重

指的是线性布局的下级视图各自拥有多大比例的宽高

layout_width填0dp时,layout_weight表示水平方向的宽度比例

layout_height同理

相对布局RelativeLayout

相对布局的下级视图位置由其他视图决定。用于确定下级视图位置的参照物分两种:

与该视图平级的视图

该视图的上级视图

如果不设置下级视图的参照物,那么下级视图默认显示在RelativeLayout内部的左上角。

63b9e869914593aeb2d3d1cfaf26388f.png

GirdLayout

1054ffa61bbe07e07091d318bc6a3484.png

滚动视图ScrollView

8ed9285717609afbab32da2cfde5c858.png

按钮Button

35f461d939352b282d93e897f3c9d735.png
069eb32f2d7e4512dd96d492568c9a01.png

4.按钮触控

f503d7f04e00f70edfcc146725eb5569.png

1.硬编码,在xml中直接指定点击事件

缺点:高耦合

2.在java代码中实现

a.可以新建一个类来实现接口

b.也可以让当前activity直接implements View.OnClickListener

c.lambda表达式实现匿名内部类.(jdk8以上可以)

以下将这四种方法都实现了

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="doClick"
        android:text="在xml中设置槽">

    </Button>

    <TextView
        android:id="@+id/tv_button_result"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="点击上面的按钮查看结果"></TextView>

    <Button
        android:id="@+id/btn_click_single"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="指定单独的点击监听器"></Button>

    <TextView
        android:id="@+id/tv_click_single_result"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="点击上面的按钮查看结果"></TextView>

    <Button
        android:id="@+id/btn_click_public"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="指定公共的点击监听器"></Button>

    <TextView
        android:id="@+id/tv_click_public_result"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="点击上面的按钮查看结果"></TextView>

    <Button
        android:id="@+id/btn_click_lambda"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="指定匿名内部内的长按点击监听器"></Button>

    <TextView
        android:id="@+id/tv_click_lambda_result"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="点击上面的按钮查看结果"></TextView>

</LinearLayout>
package com.example.helloworld;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.example.helloworld.util.DateUtil;

public class MainActivity_Button extends AppCompatActivity implements View.OnClickListener {
    private TextView tv_button_result;
    private TextView tv_click_single_result;
    private TextView tv_click_public_result;
    private TextView tv_click_lambda_result;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_button);
        tv_button_result = findViewById(R.id.tv_button_result);


        tv_click_single_result = findViewById(R.id.tv_click_single_result);
        Button btn_click_single = findViewById(R.id.btn_click_single);
        btn_click_single.setOnClickListener(new MyOnClickListener(tv_click_single_result)); //需要一个监听接口,点击后实例化一个对象,执行onClick
        //MyOnClickListener是一个静态内部类,而tv_click_single_result不是静态,因此要传值

        tv_click_public_result = findViewById(R.id.tv_click_public_result);
        Button btn_click_public = findViewById(R.id.btn_click_public);
        btn_click_public.setOnClickListener(this); //this是当前activity的实例 ,使当前activity实例implements View.OnClickListener,实现接口

        tv_click_lambda_result = findViewById(R.id.tv_click_lambda_result);
        Button btn_click_lambda = findViewById(R.id.btn_click_lambda);
        btn_click_lambda.setOnLongClickListener(view -> {
            String desc = String.format("%s 您点击了按钮:%s", DateUtil.getNowTime(), ((Button) view).getText());
            tv_click_lambda_result.setText(desc);
            return true;
        });
    }

    public void doClick(View view) {
        String desc = String.format("%s 您点击了按钮:%s", DateUtil.getNowTime(), ((Button) view).getText());
        tv_button_result.setText(desc);
    }

    @Override
    public void onClick(View view) {
        //如果有多个按钮都执行这个方法,为了区分不同按钮,要用if语句判断
        if (view.getId() == R.id.btn_click_public) {
            String desc = String.format("%s 您点击了按钮:%s", DateUtil.getNowTime(), ((Button) view).getText());
            tv_click_public_result.setText(desc);
        }
    }

    //创建一个类实现接口,实例化类
    //设置为静态类,防止内存泄漏
    static class MyOnClickListener implements View.OnClickListener {
        private final TextView tv_click_single_result;

        //构造方法
        public MyOnClickListener(TextView tv_click_single_result) {
            this.tv_click_single_result = tv_click_single_result;
        }

        @Override
        public void onClick(View view) {
            String desc = String.format("%s 您点击了按钮:%s", DateUtil.getNowTime(), ((Button) view).getText());
            tv_click_single_result.setText(desc);
        }
    }
}
a5a336c914c2727f176fb0abcde02613.png

按钮的禁用与恢复

689d2f7c9171e0497a98bdc61707fdc3.png

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:id="@+id/btn_enable"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="启用测试按钮"
            android:textSize="17sp"/>
        <Button
            android:id="@+id/btn_disable"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="禁用测试按钮"
            android:textSize="17sp"/>
    </LinearLayout>

    <Button
        android:id="@+id/btn_test"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="测试按钮"
        android:textColor="#888888"
        android:textSize="17sp"
        android:enabled="false"/>

    <TextView
        android:id="@+id/tv_result"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="查看点击结果"
        android:textColor="#000000"
        android:textSize="17sp"/>



</LinearLayout>
package com.example.helloworld;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.example.helloworld.util.DateUtil;

public class MainActivity_button_enable extends AppCompatActivity implements View.OnClickListener{

    private Button btn_test;
    private Button btn_disable;
    private TextView tv_result;
    private Button btn_enable;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_button_enable);

        btn_enable=findViewById(R.id.btn_enable);
        btn_disable = findViewById(R.id.btn_disable);
        btn_test= findViewById(R.id.btn_test);
        tv_result= findViewById(R.id.tv_result);


        btn_enable.setOnClickListener(this);
        btn_disable.setOnClickListener(this);
        btn_test.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch(view.getId()){
            case R.id.btn_enable:
                btn_test.setEnabled(true);
                btn_test.setTextColor(Color.BLACK);
                break;
            case R.id.btn_disable:
                btn_test.setEnabled(false);
                btn_test.setTextColor(Color.GRAY);
                break;
            case R.id.btn_test:
                String desc = String.format("%s 您点击了按钮:%s", DateUtil.getNowTime(), ((Button) view).getText());
                tv_result.setText(desc);
                break;
        }
    }
}
4e8459d6b6d0dd44dd12a70a1304f41a.png

5.图像显示

fdc6b55f8bfe6e8075e1dee88f4f0005.png

a7530fae10f7091042bc9192e4a69d8a.png

64545f11c42728c6565cfc4142e17e75.png

09b35e55483daf8181278221ad9751e5.png

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_51153727/article/details/129340889

安卓学习笔记一_the system requirements are not satisfied.-爱代码爱编程

从零开始学习安卓的笔记一 第一章Android大致可以分为四层架构: Linux 内核层、系统运行库层、应用框架层和应用层。安卓的版本安卓应用开发特色搭建开发环境创建并运行第一个项目 学习的资料

qt配置安卓环境(保姆级教程)_qt配置android环境-爱代码爱编程

目录 下载环境资源 JDK1.8  NDK   SDK ​安装QT 配置环境 下载环境资源 JDK1.8 介绍         JDK是Java开发的核心工具,为Java开发者提供了一套完整的开发环境,包括开发工具、类库和API等,使得开发者可以高效地编写、测试和运行Java应用程序。 下载         进入官网下载