Kotlin Android扩展| Android与Kotlin
I found that I should discuss it before moving further as we will be using it recurrently. Since we have started building our apps using kotlin, this plugin will surely make you fall in love with kotlin. If you haven’t read previous articles yet, it is recommended that you should have an eye over them to get the basic overview.
我发现我应该先讨论它,然后再继续使用,因为我们将反复使用它。 由于我们已经开始使用kotlin构建应用程序,因此该插件一定会让您爱上kotlin。 如果您还没有阅读过以前的文章 ,建议您仔细阅读它们以获得基本概述。
Kotlin-android-extension is a plugin that comes with many features such view binding, Parcelable implementation generator etc. In this article, we will discuss mainly view binding.
Kotlin-android-extension是一个具有许多功能的插件,例如视图绑定,Parcelable实现生成器等。在本文中,我们将主要讨论视图绑定。
If you have ever developed an android app using Java, you may probably get tired of using findViewById() for view binding. Mostly developer prefers Butterknife library for the same. But kotlin has made all that simple. Just enable the plugin and you are all set.
如果您曾经使用Java开发过android应用,则可能会厌倦了使用findViewById()进行视图绑定。 通常,开发人员更喜欢使用Butterknife库 。 但是Kotlin使一切变得简单。 只需启用插件即可。
配置扩展 (Configuring the extension)
Since it is a part of the Kotlin plugin for IntelliJ IDEA and Android Studio. You do not need to install any additional plugin.
由于它是IntelliJ IDEA和Android Studio的Kotlin插件的一部分。 您不需要安装任何其他插件。
All you need is to enable the Android Extensions Gradle plugin in your module's "build.gradle" file:
您需要做的就是在模块的“ build.gradle”文件中启用Android Extensions Gradle插件:
apply plugin: 'kotlin-android-extensions'
导入合成属性 (Import synthetic properties)
To include all widgets in one go, you need to write one line in the import section in kotlin file. It will automatically bind views to their respective names.
要一次性包含所有小部件,您需要在kotlin文件的import部分中写一行。 它将自动将视图绑定到其各自的名称。
import kotlinx.android.synthetic.main.<layoutName>.*
Suppose if layout name is "activity_main.xml", then you have to write:
假设布局名称为“ activity_main.xml” ,那么您必须编写:
import kotlinx.android.synthetic.main.activity_main.*
Suppose the "activity_main.xml" is as follows,
假设“ activity_main.xml”如下所示,
<LinearLayput
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/welcome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
<ImageView
android:id="@+id/imageView"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center" />
</LinearLayput>
Once we have imported all view we can use them as follows,
导入所有视图后,我们可以按以下方式使用它们:
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// for TextView
welcome.text = "Welcome user ! "
// for ImageView
imageView.setImageResource(R.drawable.ic_launcher)
}
}
The same code in java will we written as,
我们将用Java编写的相同代码编写为:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity{
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//for TextView
TextView welcome=(TextView)findViewById(R.id.welcome);
welcome.setText("Welcome User,");
// for ImageView
ImageView image = (ImageView) findViewById(R.id.imageView);
image.setImageResource(R.drawable.android_image3);
}
}
Conclusion:
结论:
So we can see how efficiently we can bind views using kotlin-android-extension. This is one of its features. If you face any doubt, feel free to write in the comment section below. In the next articles, we will try to get deeper insights into android with kotlin. Till then Good Bye and have a nice day.
因此,我们可以看到使用kotlin-android-extension绑定视图的效率如何。 这是其功能之一。 如果您有任何疑问,请随时在下面的评论部分中撰写。 在接下来的文章中,我们将尝试通过kotlin深入了解android。 直到再见,祝您有美好的一天。
翻译自: https://www.includehelp.com/kotlin/kotlin-android-extension.aspx
更多推荐

所有评论(0)