当前位置:网站首页>Learning Android from scratch -- Introduction
Learning Android from scratch -- Introduction
2022-04-23 04:46:00 【Scattered moon】
Reference books : First line of code Android
3.18 Introduction to project structure
app Code in project 、 Resources and other contents are placed in this directory , Our later development work is basically carried out under this directory .
- libs If you use a third party in your project jar package , That's what we need to do jar Bags are all placed in libs Under the table of contents , In this directory jar The package is automatically added to the project's build path .
- java The catalog is where we put all our Java Where to code (Kotlin The code is also here ), Expand the Catalog , You will see that the system automatically generates a MainActivity file .
- res There are a lot of contents in this directory . To put it simply , It's all the pictures you use in the project 、 Layout 、 word String and other resources should be stored in this directory . Of course, there are many subdirectories in this directory , Picture on drawable Under the table of contents , Layout in layout Under the table of contents , String in values Under the table of contents , So you don't have to worry about putting the whole res The catalogue is in a mess .
- AndroidManifest.xml
This is the whole thing Android The configuration file for the project , All four components you define in the program need to be noted in this file book , You can also add a permission declaration to the application in this file .
Android- Manifest.xml file
To register , Not in the AndroidManifest.xml Registered in Activity It can't be used
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Write the interface in the layout file , And then in Activity To bring in , Layout files are defined in res/layout In the catalog
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
} }
res Catalog : Put all the pictures in drawable-xxhdpi It's just in the catalog , Because this is the most popular device resolution Directory .
- all With “drawable” The first directory is used to put pictures
- All with “mipmap” The first directory is used to put application icons
- All with “values” The first directory is used to put strings 、 style 、 Color, etc
- all With “layout” The first directory is used to put layout files .
In the code through R.string.app_name You can get a reference to the string .
stay XML Pass through @string/app_name You can get a reference to the string .
build.gradle in :dependencies Closure
This closure is very powerful , It can specify all dependencies of the current project . Usually Android Studio There are 3 Ways of dependence : Local dependence 、 Library dependency and remote dependency . Local dependence can be on local jar Package or directory add dependency , Library dependencies can add dependencies to library modules in a project , Remote dependencies can jcenter Open source projects on the warehouse Add dependencies .
3.19 Logging tools
- Log.v(). For printing the most trivial 、 Least significant log information . Corresponding level verbose, yes Android The lowest level in the log .
- Log.d(). Used to print some debugging information , This information should be helpful for debugging programs and analyzing problems . Corresponding level debug, Than verbose Higher level .
- Log.i(). Used to print some important data , These data should be what you really want to see 、 I can divide it for you Analyze the data of user behavior . Corresponding level info, Than debug Higher level .
- Log.w(). Used to print some warning messages , Tip programs may have potential risks in this place , You'd better fix it Repeat these warnings . Corresponding level warn, Than info Higher level .
- Log.e(). Used to print error messages in programs , For example, the program enters catch In the sentence . When there is an error message, type When printed , Generally speaking, there is a serious problem with your program , It must be repaired as soon as possible . Corresponding level error, Than warn Higher level .
//example
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
Log.d("MainActivity", "onCreate execute")
} }
Log.d() Method passed in two parameters : The first parameter is tag, It's usually better to pass in the current class name , Mainly used in relation to Print information for filtering ; The second parameter is msg, That is, the specific content you want to print .
Don't use println() Why : The log switch is not controllable 、 Cannot add log tag 、 There is no level difference between logs …
3.20 kotlin introduction
Type declaration
- val Constant
- var Variable
val a: Int = 10
Function definition
fun main() {
val a = 10
println("a = " + a)
}
fun methodName(param1: Int, param2: Int): Int {
return 0
}
// One line of code
fun largerNumber(num1: Int, num2: Int) = if (num1 > num2) num1 else num2
//when
fun getScore(name: String) = when (name) {
"Tom" -> 86
"Jim" -> 77
"Jack" -> 95
"Lily" -> 100
else -> 0
}
//is keyword
fun checkNumber(num: Number) {
when (num) {
is Int -> println("number is Int")
is Double -> println("number is Double")
else -> println("number not support")
} }
Range val range = 0…10 Closed interval [0,10]
for (i in 0..10) {
println(i)
}
for (i in 10 downTo 1) {
println(i)
}
Initialization list 、 aggregate 、Map
//listof() Construct immutable list
val list = listOf("Apple", "Banana", "Orange", "Pear", "Grape")
for (fruit in list) {
println(fruit)
}
//mutableListof() Construct variable list
var list = mutableListOf("Apple", "Banana", "Orange", "Pear", "Grape")
list.add("Watermelon")
for (fruit in list) {
println(fruit)
}
//lambda
val maxLengthFruit = list.maxBy {
it.length }
//lambda
val newList = list.map {
it.toUpperCase() } //map The function is very powerful , It can arbitrarily map and transform the elements in the collection according to our needs , Top only It's just a simple example . besides , You can also convert all fruit names to lowercase , Or just take the first word Letter , Even a set of numbers converted into word length
// aggregate
val set = setOf("Apple", "Banana", "Orange", "Pear", "Grape")
//map
// Method 1
val map = HashMap<String, Int>()
map["Apple"] = 1
map["Banana"] = 2
map["Orange"] = 3
map["Pear"] = 4
map["Grape"] = 5
// Method 2
val map = mapOf("Apple" to 1, "Banana" to 2, "Orange" to 3, "Pear" to 4, "Grape" to 5)
//map Traverse
for ((fruit, number) in map) {
println("fruit is " + fruit + ", number is " + number)
}
Functional expression API
//filter function
val list = listOf("Apple", "Banana", "Orange", "Pear", "Grape", "Watermelon")
val newList = list.filter {
it.length <= 5 }
.map {
it.toUpperCase() }
//any,all function
val list = listOf("Apple", "Banana", "Orange", "Pear", "Grape", "Watermelon")
val anyResult = list.any {
it.length <= 5 } //true
val allResult = list.all {
it.length <= 5 } //false
// Register click events for buttons
button.setOnClickListener {
}
Nullable type
Add a question mark after the class name . such as ,Int surface Indicates a non nullable integer , and Int? Represents an integer that can be null ;String Represents a non nullable string , and String? Represents an empty string .
// Equivalent to making judgment
fun doStudy(study: Study?) {
study?.readBooks()
study?.doHomework()
}
val c = a ?: b
//let
fun doStudy(study: Study?) {
study?.let {
stu ->
stu.readBooks()
stu.doHomework()
}
}
// improvement
fun doStudy(study: Study?) {
study?.let {
it.readBooks()
it.doHomework()
}
}
String inline expression
${}, When there is only one variable in the expression , You can also omit curly braces on both sides .
版权声明
本文为[Scattered moon]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220555418305.html
边栏推荐
- Customize the navigation bar at the top of wechat applet (adaptive wechat capsule button, flex layout)
- js 判断数字字符串中是否含有字符
- Unity3D 实用技巧 - 理论知识库(一)
- What is the meaning of load balancing
- General enumeration constant class
- Youqilin 22.04 lts version officially released | ukui 3.1 opens a new experience
- Code007 -- determine whether the string in parentheses matches
- Flink's important basics
- Flink case - Kafka, MySQL source
- Introduction to raspberry pie 3B - system installation
猜你喜欢
C language: Advanced pointer
Customize the navigation bar at the top of wechat applet (adaptive wechat capsule button, flex layout)
做数据可视化应该避免的8个误区
Inverse system of RC low pass filter
Pixel 5 5g unlocking tutorial (including unlocking BL, installing edxposed and root)
泰克示波器DPO3054自校准SPC失败维修
What is a data island? Why is there still a data island in 2022?
Recursive call -- Enumeration of permutations
Installation of zynq platform cross compiler
Innovative practice of short video content understanding and generation technology in meituan
随机推荐
Eksctl deploying AWS eks
Coinbase: basic knowledge, facts and statistics about cross chain bridge
简单的拖拽物体到物品栏
leetcode003--判断一个整数是否为回文数
MySQL - data read / write separation, multi instance
SQL statement for adding columns in MySQL table
Code007 -- determine whether the string in parentheses matches
/etc/bash_ completion. D directory function (the user logs in and executes the script under the directory immediately)
Innovative practice of short video content understanding and generation technology in meituan
Unity rawimage background seamlessly connected mobile
QML进阶(五)-通过粒子模拟系统实现各种炫酷的特效
What is a blocking queue? What is the implementation principle of blocking queue? How to use blocking queue to implement producer consumer model?
leetcode006--查找字符串数组中的最长公共前缀
Summary of MySQL de duplication methods
Differences among electric drill, electric hammer and electric pick
Leetcode001 -- returns the subscript of the array element whose sum is target
Recommended scheme for national production of electronic components for wireless charging
Getprop property
leetcode004--罗马数字转整数
Special topic of data intensive application system design