Android编程权威指南(第四版)-第 1 章 Android开发初体验

文章目录

  • 代码:
    • MainActivity
    • activity_main.xml
    • string.xml

代码:

MainActivity

package com.example.geoquiz

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.Gravity
import android.view.View
import android.widget.Button
import android.widget.Toast

class MainActivity : AppCompatActivity() {
    private lateinit var ture_buttin: Button
    private lateinit var false_buttin: Button
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        ture_buttin = findViewById(R.id.ture_button)
        false_buttin = findViewById(R.id.false_button)
        ture_buttin.setOnClickListener(View.OnClickListener {
            var toast = Toast.makeText(this, R.string.ture_toast, Toast.LENGTH_SHORT)
            toast.setGravity(Gravity.TOP, 0, 0)//设置toast的弹出方向为屏幕顶部弹出,sdk版本需低于30才能生效
            toast.show()
        })

        false_buttin.setOnClickListener(View.OnClickListener {
            var toast = Toast.makeText(this, R.string.false_toast, Toast.LENGTH_SHORT)
            toast.setGravity(Gravity.TOP, 0, 0)//设置toast的弹出方向为屏幕顶部弹出
            toast.show()
        })
    }
}

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"
    android:orientation="vertical"
    android:gravity="center">

    <TextView
        android:id="@+id/question"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/question" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_gravity="center"
        android:layout_marginTop="20dp">

    <Button
        android:id="@+id/ture_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/ture_button"/>

    <Button
        android:id="@+id/false_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/false_button"/>
    </LinearLayout>
</LinearLayout>

string.xml

<resources>
    <string name="app_name">GeoQuiz</string>
    <string name="question">故宫是在北京吗?</string>
    <string name="question1">长城是在北京吗?</string>
    <string name="question2">黄鹤楼是在武汉吗?</string>
    <string name="question3">避暑山庄在武汉吗?</string>
    <string name="question4">月牙泉是在江苏吗?</string>
    <string name="question5">布达拉宫是在新疆吗?</string>
    <string name="ture_button">正确</string>
    <string name="false_button">错误</string>
    <string name="ture_toast">答对了</string>
    <string name="false_toast">答错了</string>
</resources>

你可能感兴趣的:(android)