Android (Kotlin)简单的尝试

随着kotlin对Android支持,我们多了一条开发安卓的新方式,虽然java一直是主流的,但是我们开始要多接触新事物,了解一下

(AS 3.0以上均支持kotlin插件)上码:

gradle.project

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    ext.kotlin_version = '1.3.60'
    repositories {
        google()
        jcenter()
        
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.5.3'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
        
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

 gradle.app

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.2"
    defaultConfig {
        applicationId "com.example.mykotlin"
        minSdkVersion 19
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.core:core-ktx:1.2.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}

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"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".MainActivity">

    <TextView
            android:id="@+id/tv_show"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="Hello World!"
    />
    <Button
            android:id="@+id/bt_go"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="跳转"/>

</LinearLayout>

 

这里主要是做了个页面跳转,另一个页面用的是java的

MainActivity:

package com.example.kotlin01

import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Button
import android.widget.TextView
import kotlinx.android.synthetic.main.activity_main.*


class MainActivity : AppCompatActivity() {
    val TAG = "MainActivity--->"
    var a: Int= 0
  //初始化第二种方式
  // lateinit var tv_show :TextView
  // lateinit var bt_go:Button
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        findViewById<TextView>(R.id.tv_show)
        findViewById<Button>(R.id.bt_go)
        //第二种方式
       // tv_show =findViewById(R.id.tv_show)
       // bt_go=findViewById(R.id.bt_go)

        tv_show.setText("我是谁123!!!!!")
        tv_show.setTextColor(resources.getColor(R.color.colorPrimary))
        a = int_sum(3, 4)
        System.out.println("输出的值==" + a)
        bt_go.setOnClickListener() {
            val intent = Intent(this, Main2Activity::class.java)
            intent.putExtra("a", a)
            startActivity(intent)
        }
    }

    override fun onRestart() {
        super.onRestart()
        Log.i(TAG, "onRestart")
    }

    override fun onResume() {
        super.onResume()
        Log.i(TAG, "onResume")
    }

    override fun onPause() {
        super.onPause()
        Log.i(TAG, "onPause")
    }

    override fun onStop() {
        super.onStop()
        Log.i(TAG, "onStop")
    }

    override fun onDestroy() {
        super.onDestroy()
        Log.i(TAG, "onDestroy")
    }

    fun int_sum(a: Int, b: Int): Int {
        return a + b
    }
}

跳转到新的页面(java)并接收页面(kt)传过来的值

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".Main2Activity">

    <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="跳转"
            android:onClick="ongo"
    />
</LinearLayout>

MainActivity2:

package com.example.kotlin01;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class Main2Activity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        Intent os=getIntent();
        int a = os.getIntExtra("a",0);
        Toast.makeText(this,"收到的值=="+ a ,Toast.LENGTH_LONG).show();
    }

    public void ongo(View view) {
        Intent is =new Intent(this,MainActivity.class);
        startActivity(is);
    }
}

其中一些方法的调用方式,跟java不太一样,其中我们还是要重新学习一下kotlin 的语法那些,慢慢去熟悉,虽然这边没有做到什么重要的事情,但我们可以做个对kotlin 的 一个初步了了解,方便以后的学习。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值