学习《第一行代码》第三版敲的代码
程序员文章站
2022-07-03 18:17:49
由于主要在acitivity_main.xml和MainActivity.kt, 这里一般只展示这两个然后代码用到的资源(例如图片)请自行到 第一行代码——Android(第3版)图灵社区下载4.1 至 4.2acitivity_main.xml
fruit_item.xml
由于主要在acitivity_main.xml和MainActivity.kt, 这里一般只展示这两个
然后代码用到的资源(例如图片)请自行到 第一行代码——Android(第3版)图灵社区下载
4.1 至 4.2
acitivity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="This is TextView"
android:gravity="center"
android:textColor="#00ff00"
android:textSize="24sp"
tools:ignore="HardcodedText" />
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button"
android:textSize="30sp"
android:textAllCaps="false"
tools:ignore="HardcodedText"
/>
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Type something here"
android:maxLines="2"
tools:ignore="HardcodedText" />
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/grape_pic"/>
<ProgressBar
android:id="@+id/progressBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="?android:attr/progressBarStyleHorizontal"
android:max="100"/>
</LinearLayout>
MainActivity.kt
package com.example.helloworld
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity(), View.OnClickListener {
private var flag = 0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
button.setOnClickListener(this)
}
override fun onClick(v: View?) {
when (v?.id) {
R.id.button -> {
// val inputText = editText.text.toString()
// Toast.makeText(this, inputText, Toast.LENGTH_SHORT).show()
when (flag) {
0 -> imageView.setImageResource(R.drawable.cherry_pic)
1 -> imageView.setImageResource(R.drawable.grape_pic)
2 -> imageView.setImageResource(R.drawable.watermelon_pic)
3 -> imageView.setImageResource(R.drawable.strawberry_pic)
}
flag = (flag + 1) % 4
// AlertDialog.Builder(this).apply {
// setTitle("你点开了Dialog")
// setMessage("Something important")
// setCancelable(false)
// setPositiveButton("Ok") {dialog, which ->
// }
// setNegativeButton("Cancel"){dialog, which->
// }
// show()
// }
progressBar.progress = (progressBar.progress + 25) % 110
}
}
}
}
4.4自定义组件
MainActivity.kt
package com.example.helloworld
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
class MainActivity : AppCompatActivity(){
override fun onCreate(savedInstanceState: Bundle?){
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
supportActionBar?.hide()
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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">
<com.example.helloworld.TitleLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
TitleLayout.kt
package com.example.helloworld
import android.app.Activity
import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.LinearLayout
import android.widget.Toast
import kotlinx.android.synthetic.main.title.view.*
class TitleLayout(context: Context, attrs: AttributeSet): LinearLayout(context, attrs){
init {
LayoutInflater.from(context).inflate(R.layout.title, this)
titleBack.setOnClickListener{
val activity = context as Activity
activity.finish()
}
titleEdit.setOnClickListener{
Toast.makeText(context, "you clicked Edit button", Toast.LENGTH_SHORT).show()
}
}
}
4.5ListView
MainActivity.kt
package com.example.helloworld
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.ArrayAdapter
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity(){
private val data = listOf("Apple", "Banana", "Orange", "Watermelon",
"Pear", "Grape", "Pineapple", "strawberry", "Cherry", "Mango",
"Pear", "Grape", "Pineapple", "strawberry", "Cherry")
override fun onCreate(savedInstanceState: Bundle?){
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val adapter = ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data)
listView.adapter = adapter
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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">
<ListView
android:id="@+id/listView"
android:layout_height="match_parent"
android:layout_width="match_parent"/>
</LinearLayout>
主要由下列四个文件构成
MainActivity.kt
package com.example.helloworld
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity(){
private val fruitList = ArrayList<Fruit>()
override fun onCreate(savedInstanceState: Bundle?){
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
initFruits()
val adapter = FruitAdapter(this, R.layout.fruit_item, fruitList)
listView.adapter = adapter
listView.setOnItemClickListener{_, _, position, _->
val fruit = fruitList[position]
Toast.makeText(this, fruit.name, Toast.LENGTH_SHORT).show()
}
}
private fun initFruits(){
repeat(2){
fruitList.add(Fruit("Apple", R.drawable.apple_pic))
fruitList.add(Fruit("Banana", R.drawable.banana_pic))
fruitList.add(Fruit("Orange", R.drawable.orange_pic))
fruitList.add(Fruit("Watermelon", R.drawable.watermelon_pic))
fruitList.add(Fruit("Pear", R.drawable.pear_pic))
fruitList.add(Fruit("Grape", R.drawable.grape_pic))
fruitList.add(Fruit("Pineapple", R.drawable.pineapple_pic))
fruitList.add(Fruit("Strawberry", R.drawable.strawberry_pic))
fruitList.add(Fruit("Cherry", R.drawable.cherry_pic))
fruitList.add(Fruit("Mango", R.drawable.mango_pic))
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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">
<ListView
android:id="@+id/listView"
android:layout_height="match_parent"
android:layout_width="match_parent"/>
</LinearLayout>
fruit_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="60dp">
<ImageView
android:id="@+id/fruitImage"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp"
tools:ignore="ContentDescription,RtlHardcoded" />
<TextView
android:id="@+id/fruitName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp"
tools:ignore="RtlHardcoded" />
</LinearLayout>
FruitAdapter.kt
package com.example.helloworld
import android.app.Activity
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import android.widget.ImageView
import android.widget.TextView
import android.view.LayoutInflater
class FruitAdapter(activity: Activity, val resourceId: Int, data: List<Fruit>):
ArrayAdapter<Fruit>(activity, resourceId, data){
inner class ViewHolder(val fruitImage: ImageView, val fruitName: TextView)
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
val view: View
val viewHolder: ViewHolder
if (convertView == null) {
view = LayoutInflater.from(context).inflate(resourceId, parent, false)
val fruitImage: ImageView = view.findViewById(R.id.fruitImage)
val fruitName: TextView = view.findViewById(R.id.fruitName)
viewHolder = ViewHolder(fruitImage, fruitName)
view.tag = viewHolder
} else {
view = convertView
viewHolder = view.tag as ViewHolder
}
var fruit = getItem(position)
if(fruit != null){
viewHolder.fruitImage.setImageResource(fruit.imageId)
viewHolder.fruitName.text = fruit.name
}
return view
}
}
本文地址:https://blog.csdn.net/weixin_43850253/article/details/107358042
上一篇: 为什么北方小年和南方小年不在同一天
下一篇: Okio好在哪里?
推荐阅读
-
两千行代码的PHP学习笔记汇总
-
温习Android基础知识——《第一行代码(第三版)》读书笔记 Chapter 14 高级技巧
-
《第一行代码 第2版》中新建Activity时的Backwards Compatibility 选项框,怎么不见了?
-
荐 「「第一行代码(第二版)」」学习笔记 5 碎片(连载中)
-
《第一行代码》学习笔记:更强大的滚动控件——RecyclerView(增强版的 ListView)
-
第一行代码学习笔记:跨程序共享数据----内容提供器
-
学习《第一行代码》第三版敲的代码
-
《第一行代码》阅读笔记(十九)——广播的最佳实践(强制下线功能)
-
《从零开始学Swift》学习笔记(Day1)——我的第一行Swift代码
-
温习Android基础知识——《第一行代码(第三版)》读书笔记 Chapter 10 Service