因为安卓在手上拿着,方便一点,看看能不能学一丢丢小技术,做点生活小工具。
不深入学。
android studio 的环境配置很麻烦,稀奇古怪的错误很多,不再整。
以下是一个很简单的安卓程序。具体架构我也不懂,反正这个就相当于 main.cpp 这种。
MainActivity.kt:
package com.example.testone
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.tooling.preview.Preview
import com.example.testone.ui.theme.TestoneTheme
import androidx.compose.ui.unit.dp
import androidx.compose.foundation.layout.padding
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) { // 此应用的入口
super.onCreate(savedInstanceState)
setContent { // 通过可组合函数,定义布局
TestoneTheme {
// A surface container using the 'background' color from the theme
Surface( // 界面的某一部分。这是一个容器
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colors.background
) {
Greeting("Android")
}
}
}
}
}
@Composable // 标这玩意的,可被其他函数调用,告诉编译器,xxx 可被生成 UI。这种函数无法返回任何内容
fun Greeting(name: String) {
Surface(color = Color.Magenta) {
Text(text = "Hi! my name is $name!", modifier = Modifier.padding(24.dp))
}
}
@Preview(showBackground = true) // 展示 background 。预览函数
@Composable
fun DefaultPreview() { // 很酷的功能,无序重构整个应用,便可看到效果
TestoneTheme {
Greeting("likehong")
}
}
代码如下
fun main() {
val trafficLightColor = "Yellow"
when (trafficLightColor) {
"Red" -> println("Stop")
"Yellow" -> println("Slow")
"Green" -> println("Go")
else -> println("Invalid traffic-light color")
}
}
// or
fun main() {
val x = 3
when (x) {
2 -> println("x is a prime number between 1 and 10.")
3 -> println("x is a prime number between 1 and 10.")
5 -> println("x is a prime number between 1 and 10.")
7 -> println("x is a prime number between 1 and 10.")
else -> println("x isn't a prime number between 1 and 10.")
}
}
fun main() {
val x = 3
when (x) {
2, 3, 5, 7 -> println("x is a prime number between 1 and 10.")
in 1..10 -> println("x is a number between 1 and 10, but not a prime number.")
else -> println("x isn't a prime number between 1 and 10.")
}
}
fun main() {
val x: Any = 20
when (x) {
2, 3, 5, 7 -> println("x is a prime number between 1 and 10.")
in 1..10 -> println("x is a number between 1 and 10, but not a prime number.")
is Int -> println("x is an integer number, but not between 1 and 10.")
else -> println("x isn't an integer number.")
}
}
不过我现在对语法不感兴趣,明天或有时间了再学。
// activaty_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="34sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="1" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="@string/button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />
</androidx.constraintlayout.widget.ConstraintLayout>
// MainActivity.kt
package com.example.side_learn1
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main) // 主布局文件
/* 找到 UI 文件 */
val rollButton: Button = findViewById(R.id.button2)
rollButton.setOnClickListener {
rollDice()
}
}
private fun rollDice() {
/* 掷色子 */
val dice = Dice(6)
val diceRoll = dice.roll()
val resultTextView: TextView = findViewById(R.id.textView)
resultTextView.text = diceRoll.toString()
}
}
class Dice(val numSides: Int) {
fun roll(): Int {
return (1..6).random()
}
}