当前位置:网站首页>Kotlin a simple Android program
Kotlin a simple Android program
2022-06-22 07:51:00 【Lord Song - Focus】
In the last week https://www.kotlincn.net/docs/reference/ To study the Kotlin, Is in eclipse Installed plug-ins for practice , After practicing the knowledge in the code , It works Kotlin Development android Impulse .
This simple program realizes the function of making a call
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.song.androidart">
<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
<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/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Layout file
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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="com.song.androidart.MainActivity">
<TextView android:id="@+id/titleN" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
The above code is unchanged
Look down here Kotlin Written Activity Well
class MainActivity : AppCompatActivity() {
val callRequestCode:Int=10;
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setName("call phone")
}
fun setName(name:String){
titleN.text=name;
titleN.setOnClickListener{
if(ContextCompat.checkSelfPermission(this,android.Manifest.permission.CALL_PHONE)!=PackageManager.PERMISSION_GRANTED){
var str= arrayOf(android.Manifest.permission.CALL_PHONE);
ActivityCompat.requestPermissions(this, str,callRequestCode);
}
else {
call();
}
};
}
fun call (){
var mIntent=Intent();
mIntent.setAction(Intent.ACTION_CALL);
mIntent.setData(Uri.parse("tel:10086"));
startActivity(mIntent);
}
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
when(requestCode){
callRequestCode->{
if(grantResults[0]==PackageManager.PERMISSION_GRANTED){
call();
}
else {
Toast.makeText(this," Permission application failed ",Toast.LENGTH_LONG).show();
}
}
}
}
}
I have implemented two operations above , One is for TextView Set up text, There is another one by clicking TextView Realize the function of calling , Will find and java Some obvious differences between
1 Declaration of variables and constants
declare constant val Constant name : constant type = value ; for example val callRequestCode:Int=10;
Declaration of variables var Variable name : Variable type = value ; for example var age:Int=10;
The difference between constant and variable declarations is that a constant is val Variable var
2. From the above code, we can clearly find that Kotlin Realized Activity There is no findViewById The operation of , But directly through view Of id To operate , This will make our code simpler , And it will greatly improve the efficiency of development .
3. Yes view Click event handling
titleN.setOnClickListener{
if(ContextCompat.checkSelfPermission(this,android.Manifest.permission.CALL_PHONE)!=PackageManager.PERMISSION_GRANTED){
var str= arrayOf(android.Manifest.permission.CALL_PHONE);
ActivityCompat.requestPermissions(this, str,callRequestCode);
}
else {
call();
}
}; adopt setOnClickListener{} It directly implements code logic
There is another kind. Implementation interface
View.OnClickListener
titleN.setOnClickListener(this);
class MainActivity : AppCompatActivity(), View.OnClickListener // There is no interface implementation here java Of implements keyword
// How to implement the interface :
override fun onClick(v: View?) {
//kotlin Conditional judgment statements in Only if and when
when(v?.id){
R.id.titleN->{
if(ContextCompat.checkSelfPermission(this,android.Manifest.permission.CALL_PHONE)!=PackageManager.PERMISSION_GRANTED){
var str= arrayOf(android.Manifest.permission.CALL_PHONE);
ActivityCompat.requestPermissions(this, str,callRequestCode);
}
else {
call();
}
}
}
}
From this simple small project, we can see Kotlin and Java There is still a big difference , And later android The mainstream language of development may be Kotlin 了 , More insights should be learned and practiced in the project , I hope I don't mislead you , I hope you can correct any improper points in time
边栏推荐
- Usage and understanding of async/await in JS
- 模电实验——实验一 晶体管共射极单管放大器
- What are uniapp, wap2app and 5 + app in dccloud?
- Fmdb usage details mark
- CollectionViewCell
- Vue page caching problem solving (keep alive + page level routing guard + lifecycle activated)
- Open version - order delivery
- Crmeb mall distribution function
- Open version - user level
- 微信小游戏(二)
猜你喜欢

mysql查询group by 1055 问题完美解决,最简单最便捷的方法

模電實驗——實驗二 JFET共源極放大電路

Get through - coupons

Microsoft Remote Desktop 10.7.6 official

LR 2022 ultra detailed installation tutorial "latest"

Phpcms mobile portal configuration

How to backup the treasures in the store and upload them to multiple stores

对于mysql中数据为NULL引发的一些问题和思考

phpcms手机门户网站配置

How to batch copy babies by sales volume in Taoying
随机推荐
Wechat games (3)
Runloop detail summary
Xlua environment configuration
Open version - order delivery
AutoCAD 2020.3 Chinese Version (old version)
Baidu Post Bar crawler crawls to the middle of the building
Open source get through version - integral function
Modular import and export collation in JS
Common array operations in JS
Cocoapods creates private libraries and publishes them
How to cancel crmeb customer service link
itertools 排列组合
A glimpse of easy rule
The ranking of websites is very important for websites
Docker command, docker installation sqlserver2019, docker installation MySQL (continuous update)
Blob format problems involved in image uploading
UI draw line
enable_ irq_ Wake interrupt wakes up the kernel in low power mode
Remote Desktop Manager
Wx applet vant UI call interface to realize two-level cascade