当前位置:网站首页>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
边栏推荐
- Installation and use of Apache bench (AB pressure test tool)
- Installation of zynq platform cross compiler
- win10, mysql-8.0.26-winx64. Zip installation
- What is the thirty-six plan
- leetcode002--将有符号整数的数字部分反转
- Experience summary and sharing of the first prize of 2021 National Mathematical Modeling Competition
- JS generates a specified number of characters according to some words
- [timing] empirical evaluation of general convolution and cyclic networks for sequence modeling based on TCN
- What is a data island? Why is there still a data island in 2022?
- Custom switch control
猜你喜欢
Go reflection rule
做数据可视化应该避免的8个误区
解决ValueError: Argument must be a dense tensor: 0 - got shape [198602], but wanted [198602, 16].
Unity RawImage背景无缝连接移动
补充番外14:cmake实践项目笔记(未完待续4/22)
Innovation training (VI) routing
Spark small case - RDD, spark SQL
383. Ransom letter
win10, mysql-8.0.26-winx64. Zip installation
2021数学建模国赛一等奖经验总结与分享
随机推荐
Innovation training (IV) preliminary preparation - server
Wechat payment function
Raspberry pie + opencv + opencv -- face detection ------- environment construction
leetcode005--原地删除数组中的重复元素
程序员抱怨:1万2的工资我真的活不下去了,网友:我3千咋说
Arduino UNO r3+LCD1602+DHT11
io. Platform. packageRoot; // ignore: deprecated_ Member_ use
Leetcode002 -- inverts the numeric portion of a signed integer
List remove an element
Solutions to the failure of sqoop connection to MySQL
Special topic of data intensive application system design
The 14th issue of HMS core discovery reviews the long article | enjoy the silky clip and release the creativity of the video
Phishing for NFT
No such file or directory problem while executing shell
Windows remote connection to redis
C# List字段排序含有数字和字符
List&lt; Map&gt; Replication: light copy and deep copy
520. Detect capital letters
Practice and exploration of knowledge map visualization technology in meituan
C language: Advanced pointer