ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Kotlin开发第二天,探究Activity

2021-10-27 15:59:48  阅读:265  来源: 互联网

标签:Kotlin Activity 探究 Learn2Activity activity import onCreate AppCompatActivity


完整代码Gitee地址:kotlin-demo: 15天Kotlin学习计划

第二天学习内容代码:Chapter2类

 

知识点1:手动创建Activity

        Activity是最容易吸引用户的地方,它是一种可以包含用户界面的组件,主要用于和用户进行交互。一个应用程序中可以包含零个或多个Activity。

1,手动创建Activity,继承AppCompatActivity,初始化onCreate方法;

package com.example.kotlin_demo

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity

/**
 * Created by: PeaceJay
 * Created date: 2021/10/27
 * Description: 第二天 探究Activity
 * 知识点1:手动创建Activity
 */
class Learn2Activity : AppCompatActivity(){
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_learn2)
    }
}

2,创建布局文件activity_learn2,并在onCreate里使用setContentView()方法引用;

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

</androidx.constraintlayout.widget.ConstraintLayout>

 创建布局文件activity_main,并增加点击按钮控件

<?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="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity"
    android:padding="10dp">

    <androidx.appcompat.widget.AppCompatButton
        android:id="@+id/chapter2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Chapter2" />

</LinearLayout>

3,新建的Activity都需要在AndroidManifest.xml里引用;

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.kotlin_demo">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.Kotlindemo">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <!-- 第二天 探究Activity -->
        <activity android:name=".Learn2Activity"/>
    </application>

</manifest>

4,MainActivity布局增加点击按钮,并跳转Learn2Activity;

package com.example.kotlin_demo

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        //第二天学习内容
        chapter2()
    }

    private fun chapter2() {
        //获取布局资源id
        val button:Button = findViewById(R.id.chapter2)
        button.setOnClickListener{
            //跳转Learn2Activity
            //val activity = Intent(this,Learn2Activity::class.java)
            //startActivity(activity)
            //精简为
            startActivity(Intent(this,Learn2Activity::class.java))
        }
    }
}

5,设置themes.xml文件,DarkActionBar样式改为NoActionBar;

<style name="Theme.Kotlindemo" parent="Theme.MaterialComponents.DayNight.NoActionBar">

运行效果:

 

 未完待续,持续更新中ing......

 

标签:Kotlin,Activity,探究,Learn2Activity,activity,import,onCreate,AppCompatActivity
来源: https://blog.csdn.net/qq_30998053/article/details/120994543

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有