当前位置:网站首页>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

原网站

版权声明
本文为[Lord Song - Focus]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202220534015390.html