当前位置:网站首页>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
边栏推荐
- New year's Day mall material Icon
- Relative positioning, absolute positioning, fixed positioning
- 8、 Slider assembly
- 充电宝的玄机
- How to import and upload a CSV generated by a third-party platform to a Taobao store
- FFMPEG坑
- MySQL intercepts the string cs0000_ 1 medium_ Following characters
- 微信小游戏(二)
- Simplicity is the best method of network promotion
- Xlua environment configuration
猜你喜欢

Open version - inventory description

由浅入深拿下OpenFeign

Some problems caused by null data in MySQL

How to import and upload a CSV generated by a third-party platform to a Taobao store

How to batch copy babies by sales volume in Taoying

mapTalks:基础操作与WMS/WMTS地图服务加载

什么是分布式事务

Get through version 4.3 mind map

Problems caused by canvas palette width and height and canvas width and height

What are the functions of Taobao batch copy tool?
随机推荐
Semaphore
充电宝的玄机
Modular import and export collation in JS
Wechat applets will directly open the parent element when the child element of flex:1 is too long (the text is too long)
Qualcomm platform LCM summary
The applet uses the step bar vant steps in vant
各大企业连连出现亏损,环保行业何去何从?
Blob format problems involved in image uploading
An example shows the difference between let and VaR
Charles uses
Xmind 2022思维导图激活版资源?
phpcms手机门户网站配置
【宋红康 MySQL数据库 】【高级篇】【06】MySQL的逻辑架构
Open version - write off
Use js to download the current image
【圖論常見模板題】4種最短路解法和2種最小生成樹解法
The ranking of websites is very important for websites
代码覆盖率测试对编程小白的意义及其使用方法
Wechat games (5)
模电实验——实验二 JFET共源极放大电路