创建项目geoquiz
选择EmptyActivity
编程语言选择Kotlin
修改app/src/main/java/MainActivity.kt
package com.bignerdranch.android.geoquiz
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.*
class MainActivity : AppCompatActivity() {
private lateinit var trueButton:Button
private lateinit var falseButton:Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
trueButton = findViewById(R.id.true_button)
falseButton = findViewById(R.id.false_button)
trueButton.setOnClickListener{
view: View ->
//do something
Toast.makeText(this,R.string.correct_toast,Toast.LENGTH_SHORT).show()
}
falseButton.setOnClickListener{
view:View->
//do somethng
Toast.makeText(this,R.string.incorrect_toast,Toast.LENGTH_SHORT).show()
}
}
}
修改app/src/main/res/layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:padding="24dp"
android:text="@string/question_text" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/true_button"
style="?android:attr/buttonBarButtonStyle"
android:id="@+id/true_button"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/false_button"
style="?android:attr/buttonBarButtonStyle"
android:id="@+id/false_button"/>
</LinearLayout>
</LinearLayout>
修改app/src/main/res/values/strings.xml
<resources>
<string name="app_name">GeoQuiz</string>
<string name="question_text">问题1</string>
<string name="true_button">是</string>
<string name="false_button">否</string>
<string name="correct_toast">正确</string>
<string name="incorrect_toast">错误</string>
</resources>
设置AVD模拟器,运行项目
添加更多的题目
新增app/src/main/java/Question.kt
package com.bignerdranch.android.geoquiz
import androidx.annotation.StringRes
data class Question(@StringRes val textResId:Int,val answer:Boolean)
修改app/src/main/res/layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="24dp"
android:id="@+id/question_text_view"
tools:text="@string/question_1"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/true_button"
style="?android:attr/buttonBarButtonStyle"
android:id="@+id/true_button"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/false_button"
style="?android:attr/buttonBarButtonStyle"
android:id="@+id/false_button"/>
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/next_button"
style="?android:attr/buttonBarButtonStyle"
android:id="@+id/next_button"/>
</LinearLayout>
修改app/src/main/res/values/strings.xml
<resources>
<string name="app_name">GeoQuiz</string>
<string name="question_1">问题1</string>
<string name="question_2">问题2</string>
<string name="question_3">问题3</string>
<string name="question_4">问题4</string>
<string name="next_button">下一题</string>
<string name="true_button">是</string>
<string name="false_button">否</string>
<string name="correct_toast">正确</string>
<string name="incorrect_toast">错误</string>
</resources>
修改app/src/main/java/MainActivity.kt
package com.bignerdranch.android.geoquiz
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.*
class MainActivity : AppCompatActivity() {
private lateinit var trueButton:Button
private lateinit var falseButton:Button
private lateinit var nextButton:Button
private lateinit var questionTextView: TextView
private val questionBank = listOf(
Question(R.string.question_1,true),
Question(R.string.question_2,false),
Question(R.string.question_3,true),
Question(R.string.question_4,false),
)
private var currentIndex = 0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
trueButton = findViewById(R.id.true_button)
falseButton = findViewById(R.id.false_button)
nextButton = findViewById(R.id.next_button)
questionTextView = findViewById(R.id.question_text_view)
trueButton.setOnClickListener{
view: View ->
//Toast.makeText(this,R.string.correct_toast,Toast.LENGTH_SHORT).show()
checkAnswer(true)
}
falseButton.setOnClickListener{
view:View->
//Toast.makeText(this,R.string.incorrect_toast,Toast.LENGTH_SHORT).show()
checkAnswer(false)
}
nextButton.setOnClickListener{
currentIndex = (currentIndex + 1) % questionBank.size
updateQuestion()
}
updateQuestion()
}
private fun updateQuestion(){
val questionTextResId = questionBank[currentIndex].textResId
questionTextView.setText(questionTextResId)
}
private fun checkAnswer(userAnswer:Boolean){
val correctAnswer = questionBank[currentIndex].answer
val messageResId = if(userAnswer == correctAnswer){
R.string.correct_toast
}else{
R.string.incorrect_toast
}
Toast.makeText(this,messageResId,Toast.LENGTH_SHORT).show()
}
}
运行项目
添加图标资源
Activity的生命周期
引入Jetpack库的ViewModel
为什么需要ViewModel
Jetpack库
jetpack官方
一个界面对应一个Activity,下面创建第二个界面,用于显示当前问题的答案
先打开res/values/strings.xml
文件,要用的所有字符串资源
<resources>
<string name="app_name">GeoQuiz</string>
<string name="question_1">问题1</string>
<string name="question_2">问题2</string>
<string name="question_3">问题3</string>
<string name="question_4">问题4</string>
<string name="next_button">下一题</string>
<string name="true_button">是</string>
<string name="false_button">否</string>
<string name="correct_toast">正确</string>
<string name="incorrect_toast">错误</string>
<string name="warning_text">确认操作吗?</string>
<string name="show_answer_button">显示答案</string>
<string name="cheat_button">作弊!</string>
<stinrg name="judgment_toast">作弊是错误的</stinrg>
</resources>
创建新的activity至少涉及三个文件:Kotlin类文件
、XML 布局文件
和manifest 文件
。 这 三个文件关联密切,搞错了就有大麻烦。强烈建议使用IDEA
的新建 activity向导功能。
使用向导操作后会自动添加相应的res/layout/activity_cheat.xml
文件和app/src/main/java/CheatActivity.kt
文件
下面开始构建UI
修改res/layout/activity_cheat.xml
文件内容
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:gravity="center"
android:orientation="vertical"
tools:context=".CheatActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="24dp"
android:id="@+id/warning_text_view"
tools:text="@string/warning_text"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="24dp"
android:id="@+id/answer_text_view"
tools:text="回答"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/show_answer_button"
android:text="@string/show_answer_button"/>
</LinearLayout>
接着检查一下app/main/manifest.xml
配置文件中是否声明了 CheatActivity
(向导操作默认会有)
内容大概如下
<activity
android:name=".CheatActivity"
android:exported="false"/>
在默认布局中添加作弊!
按钮(app/src/main/res/layout/activity_main.xml
)
加入如下代码块
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/cheat_button"
style="?android:attr/buttonBarButtonStyle"
android:id="@+id/cheat_button"/>
完整代码如下
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="24dp"
android:id="@+id/question_text_view"
tools:text="@string/question_1"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/true_button"
style="?android:attr/buttonBarButtonStyle"
android:id="@+id/true_button"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/false_button"
style="?android:attr/buttonBarButtonStyle"
android:id="@+id/false_button"/>
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/cheat_button"
style="?android:attr/buttonBarButtonStyle"
android:id="@+id/cheat_button"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/next_button"
style="?android:attr/buttonBarButtonStyle"
android:id="@+id/next_button"/>
</LinearLayout>
修改MainActivity.kt
加入cheatButton
的控制处理代码
package com.bignerdranch.android.geoquiz
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.*
class MainActivity : AppCompatActivity() {
private lateinit var trueButton:Button
private lateinit var falseButton:Button
private lateinit var nextButton:Button
private lateinit var cheatButton: Button
private lateinit var questionTextView: TextView
private val questionBank = listOf(
Question(R.string.question_1,true),
Question(R.string.question_2,false),
Question(R.string.question_3,true),
Question(R.string.question_4,false),
)
private var currentIndex = 0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
trueButton = findViewById(R.id.true_button)
falseButton = findViewById(R.id.false_button)
nextButton = findViewById(R.id.next_button)
cheatButton = findViewById(R.id.cheat_button)
questionTextView = findViewById(R.id.question_text_view)
trueButton.setOnClickListener{
view: View ->
//Toast.makeText(this,R.string.correct_toast,Toast.LENGTH_SHORT).show()
checkAnswer(true)
}
falseButton.setOnClickListener{
view:View->
//Toast.makeText(this,R.string.incorrect_toast,Toast.LENGTH_SHORT).show()
checkAnswer(false)
}
nextButton.setOnClickListener{
currentIndex = (currentIndex + 1) % questionBank.size
updateQuestion()
}
cheatButton.setOnClickListener{
//do something
}
updateQuestion()
}
private fun updateQuestion(){
val questionTextResId = questionBank[currentIndex].textResId
questionTextView.setText(questionTextResId)
}
private fun checkAnswer(userAnswer:Boolean){
val correctAnswer = questionBank[currentIndex].answer
val messageResId = if(userAnswer == correctAnswer){
R.string.correct_toast
}else{
R.string.incorrect_toast
}
Toast.makeText(this,messageResId,Toast.LENGTH_SHORT).show()
}
}
接着来看看如何启动CheatActivity
一个activity启动另一个activity最简单的方式是使用startActivity(Intent)函数
cheatButton.setOnClickListener{
val intent = Intent(this,CheatActivity::class.java)
startActivity(intent)
}
使用intent extra
在CheatActivity.kt
中,为extra数据信息新增键值对中的键
private const val EXTRA_ANSWER_IS_TRUE = "com.bignerdranch.android.geoquiz.answer_is_strue"
CheatActivity.kt
代码如下文章来源:https://www.toymoban.com/news/detail-611929.html
package com.bignerdranch.android.geoquiz
import android.content.Context
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
private const val EXTRA_ANSWER_IS_TRUE = "com.bignerdranch.android.geoquiz.answer_is_strue"
class CheatActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_cheat)
}
companion object {
fun newIntent(packageContext: Context, answerIsTrue: Boolean): Intent {
return Intent(packageContext,CheatActivity::class.java).apply {
putExtra(EXTRA_ANSWER_IS_TRUE, answerIsTrue)
}
}
}
}
在MainActivity的按钮监听器中使用newIntent()函数,修改MainActivity.kt
文章来源地址https://www.toymoban.com/news/detail-611929.html
到了这里,关于Android开发初体验的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!