Android studio 入门笔记

记录自学Android 中新工程的创建以及制作的第一个APP

刚入坑的时候仅仅这个软件折腾了我三天之久,什么JAVA JDK啊,什么Gradle版本问题,对于一个机械的学生来说,无疑是一头雾水;当然,入门来说,什么东西都是0基础,无疑会走很多弯路,总结一句来说,开发一定要静下心来,要细心,不可盲目求成。学习Android 的兴趣完全来于 机智云 APP开源框架的修改,从此一发不可收拾,废话不多说,下面直接开撸。

注:以下内容仅仅是为了记录自己的学习笔记,以及提供给一些入门小白的捷径,如有错误 还请指出!

1.新建Android工程

图中可以看到,打开软件后的几个选项,我们只需点击第一个(启动一个新的安卓项目)。ff在这里插入图片描述
点击创建工程后来到新的页面,如图所示,此处为选择Activity界面,可根据自己需求选择,新手推荐选用第二个!选择以后点击NEXT即可。
你好啊
接下来是对工程文件的命名,以及系统版本的选择,“Name”大家都不陌生吧,更改为自己需要的即可,文件保存路径不可以有中文,推荐大家创建一个新的文件夹用来存放自己的Android工程,至于其他的选项大家跟着选择就行啦。随后只需点击Finidh即可。在这里插入图片描述
这样一个新的Android工程就创建完毕了,接下来就可以尽情发挥自己啦。
在这里插入图片描述

2.APP制作

既然是第一个APP,那就毫无技术可言啦,这里就做个类似鞋子商城的小APP吧。先预览一下后期效果!

在这里插入图片描述
在这里插入图片描述

2.1 新建一个Activity

图中可以看到APP有两个页面,每个页面都会对应一个Activity;所以除了文件生成的Main_Activity之外,还要有一个Activity与另一个页面对应,而每个Activity的XML文件都会有对应JAVA文件。
在这里插入图片描述
如上图所示,我们找到工程的Layout 文件,会看到Main_Activity的XML文件,我们新建的XML文件也要放在Layout里面。
在这里插入图片描述
鼠标选中Layout文件夹右击,在出现的对话框中按照上图选择来到如下页面,对话框中输入新建XML文件的名字,大家尽量规范的输入Activity的名字,以增加程序的可读性。
在这里插入图片描述
有了XML文件后,还要有对应的JAVA文件,下面我们来到工程的JAVA文件夹下创建JAVA文件,如下图所示,同样的右击鼠标NEW一个工程。在这里插入图片描述
创建完成后就可以在对应XML文件中编写UI了。

2.2UI编写

关于UI 的编写这里就不作过多介绍了,直接贴代码吧。

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

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/white"
        android:fadingEdge="vertical"
        android:paddingBottom="10dp"
        android:paddingTop="20dp"
        android:scrollbars="vertical" >

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

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="120dp">
            <ImageView
                android:layout_alignParentLeft="true"
                android:layout_marginLeft="20dp"
                android:layout_width="80dp"
                android:id="@+id/image_1"
               android:layout_centerVertical="true"
                android:src="@drawable/a"
                android:layout_height="80dp">
            </ImageView>
            <TextView
                android:layout_width="50dp"
                android:layout_height="30dp"
                android:text="$899"
                android:textColor="@color/colorAccent"
                android:gravity="center"
                android:layout_marginLeft="100dp"
                android:layout_marginTop="70dp"
                android:textSize="16sp"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="NIKE1"
                android:layout_centerHorizontal="true"
                android:gravity="center"
                android:layout_marginLeft="80dp"
                android:layout_marginTop="40dp"
                android:textSize="23sp"/>

        </RelativeLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="120dp">
            <ImageView
                android:layout_alignParentLeft="true"
                android:layout_marginLeft="20dp"
                android:layout_width="80dp"
                android:id="@+id/image_2"
                android:layout_centerVertical="true"
                android:src="@drawable/b"
                android:layout_height="80dp">
            </ImageView>
            <TextView
                android:layout_width="50dp"
                android:layout_height="30dp"
                android:text="$1199"
                android:textColor="@color/colorAccent"
                android:gravity="center"
                android:layout_marginLeft="100dp"
                android:layout_marginTop="70dp"
                android:textSize="16sp"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_marginLeft="80dp"
                android:layout_marginTop="40dp"
                android:gravity="center"
                android:text="NIKE2"
                android:textSize="23sp" />

        </RelativeLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="120dp">

            <ImageView
                android:id="@+id/image_3"
                android:layout_width="80dp"
                android:layout_height="80dp"
                android:layout_alignParentLeft="true"
                android:layout_centerVertical="true"
                android:layout_marginLeft="20dp"
                android:src="@drawable/c">

            </ImageView>

            <TextView
                android:layout_width="50dp"
                android:layout_height="30dp"
                android:layout_marginLeft="100dp"
                android:layout_marginTop="70dp"
                android:gravity="center"
                android:text="$1399"
                android:textColor="@color/colorAccent"
                android:textSize="16sp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_marginLeft="80dp"
                android:layout_marginTop="40dp"
                android:gravity="center"
                android:text="NIKE3"
                android:textSize="23sp" />

        </RelativeLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="120dp">
            <ImageView
                android:layout_alignParentLeft="true"
                android:layout_marginLeft="20dp"
                android:layout_width="80dp"
                android:id="@+id/image_4"
                android:layout_centerVertical="true"
                android:src="@drawable/d"
                android:layout_height="80dp">
            </ImageView>
            <TextView
                android:layout_width="50dp"
                android:layout_height="30dp"
                android:text="$999"
                android:textColor="@color/colorAccent"
                android:gravity="center"
                android:layout_marginLeft="100dp"
                android:layout_marginTop="70dp"
                android:textSize="16sp"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="NIKE4"
                android:layout_centerHorizontal="true"
                android:gravity="center"
                android:layout_marginLeft="80dp"
                android:layout_marginTop="40dp"
                android:textSize="23sp"/>

        </RelativeLayout>
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="120dp">
            <ImageView
                android:layout_alignParentLeft="true"
                android:layout_marginLeft="20dp"
                android:layout_width="80dp"
                android:id="@+id/image_5"
                android:layout_centerVertical="true"
                android:src="@drawable/f"
                android:layout_height="80dp">
            </ImageView>
            <TextView
                android:layout_width="50dp"
                android:layout_height="30dp"
                android:text="$1499"
                android:textColor="@color/colorAccent"
                android:gravity="center"
                android:layout_marginLeft="100dp"
                android:layout_marginTop="70dp"
                android:textSize="16sp"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="NIKE5"
                android:layout_centerHorizontal="true"
                android:gravity="center"
                android:layout_marginLeft="80dp"
                android:layout_marginTop="40dp"
                android:textSize="23sp"/>

        </RelativeLayout>
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="120dp">
            <ImageView
                android:layout_alignParentLeft="true"
                android:layout_marginLeft="20dp"
                android:layout_width="80dp"
                android:id="@+id/image_6"
                android:layout_centerVertical="true"
                android:src="@drawable/i"
                android:layout_height="80dp">
            </ImageView>
            <TextView
                android:layout_width="50dp"
                android:layout_height="30dp"
                android:text="$1099"
                android:textColor="@color/colorAccent"
                android:gravity="center"
                android:layout_marginLeft="100dp"
                android:layout_marginTop="70dp"
                android:textSize="16sp"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="NIKE6"
                android:layout_centerHorizontal="true"
                android:gravity="center"
                android:layout_marginLeft="80dp"
                android:layout_marginTop="40dp"
                android:textSize="23sp"/>

        </RelativeLayout>
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="120dp">
            <ImageView
                android:layout_alignParentLeft="true"
                android:layout_marginLeft="20dp"
                android:layout_width="80dp"
                android:id="@+id/image_7"
                android:layout_centerVertical="true"
                android:src="@drawable/j"
                android:layout_height="80dp">
            </ImageView>
            <TextView
                android:layout_width="50dp"
                android:layout_height="30dp"
                android:text="$999"
                android:textColor="@color/colorAccent"
                android:gravity="center"
                android:layout_marginLeft="100dp"
                android:layout_marginTop="70dp"
                android:textSize="16sp"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="NIKE7"
                android:layout_centerHorizontal="true"
                android:gravity="center"
                android:layout_marginLeft="80dp"
                android:layout_marginTop="40dp"
                android:textSize="23sp"/>

        </RelativeLayout>
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="120dp">
            <ImageView
                android:layout_alignParentLeft="true"
                android:layout_marginLeft="20dp"
                android:layout_width="80dp"
                android:id="@+id/image_8"
                android:layout_centerVertical="true"
                android:src="@drawable/h"
                android:layout_height="80dp">
            </ImageView>
            <TextView
                android:layout_width="50dp"
                android:layout_height="30dp"
                android:text="$899"
                android:textColor="@color/colorAccent"
                android:gravity="center"
                android:layout_marginLeft="100dp"
                android:layout_marginTop="70dp"
                android:textSize="16sp"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="NIKE8"
                android:layout_centerHorizontal="true"
                android:gravity="center"
                android:layout_marginLeft="80dp"
                android:layout_marginTop="40dp"
                android:textSize="23sp"/>

        </RelativeLayout>
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="120dp">
            <ImageView
                android:layout_alignParentLeft="true"
                android:layout_marginLeft="20dp"
                android:layout_width="80dp"
                android:id="@+id/image_9"
                android:layout_centerVertical="true"
                android:src="@drawable/g"
                android:layout_height="80dp">
            </ImageView>
            <TextView
                android:layout_width="50dp"
                android:layout_height="30dp"
                android:text="$1599"
                android:textColor="@color/colorAccent"
                android:gravity="center"
                android:layout_marginLeft="100dp"
                android:layout_marginTop="70dp"
                android:textSize="16sp"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="NIKE9"
                android:layout_centerHorizontal="true"
                android:gravity="center"
                android:layout_marginLeft="80dp"
                android:layout_marginTop="40dp"
                android:textSize="23sp"/>

        </RelativeLayout>

    </LinearLayout>

    </ScrollView>

    </LinearLayout>


对应的Java文件代码:

package com.example.myshoe;

import android.content.Intent;
import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;

import android.view.View;
import android.widget.ImageView;


/**
 *
 */
public class Main2_Activity extends AppCompatActivity {

    private ImageView image_1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main2_activity);

        image_1 = findViewById(R.id.image_1); // 寻找xml里面真正的id  与自己定义的id绑定
        image_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                
            }
        });
    }
}

MainActivity.JAVA代码:

package com.example.myshoe;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private Button btn_1;

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

        btn_1 = findViewById(R.id.btn_1); // 寻找xml里面真正的id  与自己定义的id绑定

        btn_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                 Intent intent = new
                Intent(MainActivity.this, Main2_Activity.class);//this前面为当前activty名称,class前面为要跳转到得activity名称
                startActivity(intent);
            }
        });

    }
}

至此APP的结构就出来啦,但是如果这样下载到手机上是无法运行的,我们新建的Acticity还并未加入到软件中,就像我们使用单片机的时候仅仅只是完成了IO的初始化和函数的编写,但是未在主函数调用,那么代码写的再完美也不过是一稽之谈,最后还要在工程的AndroidMainFest.XML文件中声明新建Activity文件:

在这里插入图片描述

3. APP打包下载

本人手机不支持真机调试,所以从接触Android开始就使用打包的方式验证APP是否可运行,相比于真机和虚拟机来说较为烦恼,大家可根据自己选择或跳过此步骤。所有的代码完成以后,且软件无报错的情况下,则可点击软件上方Build对软件进行打包;如图所示:

在这里插入图片描述
2. 选择APK文件
在这里插入图片描述
3.新建JKS密钥信息
在这里插入图片描述
4.填入相应信息
在这里插入图片描述
5.点击Finish,大功告成!在这里插入图片描述
6.程序运行无误,点击右下方locate即可跳转到APK文件夹在这里插入图片描述
7.复制APK文件到手机即可。在这里插入图片描述

4. 总结

不管是安卓开发也好,还是单片机开发也好,总之一定要细心细心再细心,这样会避免走很多弯路,更不可盲目求快,所有代码一笔带过后,再回过头调试找BUG,这样不仅效率低,而且出错的机率更大,要养成及时验证代码的习惯,因为指不定加上哪句话后,整个工程就崩溃了。第一次发帖,还有很多功能没能整明白,有需要代码的同学可以私信留言哦!

  • 10
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值