Android: Kotlin with Butterknife

前端 未结 10 879
鱼传尺愫
鱼传尺愫 2020-12-24 04:55

I\'m trying to use Kotlin with Butterknife for my Android Application.

Here is my build.gradle

dependencies {
    ...
    compile \'com.jakewharton:b         


        
相关标签:
10条回答
  • 2020-12-24 05:47

    In your gradle:

    compile 'com.jakewharton:butterknife:8.8.0'
    kapt "com.jakewharton:butterknife-compiler:8.8.0"
    

    In your activity

    @BindView(R.id.toolbar)
    lateinit var mToolbar: Toolbar
    

    Of course, remember ButterKnife.bind(this) and apply the plugin on the top of your app.gradle apply plugin: 'kotlin-kapt'

    Check full example

    Full Link: https://github.com/JetBrains/kotlin-examples/tree/master/gradle/android-butterknife

    0 讨论(0)
  • 2020-12-24 05:47

    You need to simply add ButterKnife.kt into your source tree from the following link:
    https://github.com/JakeWharton/kotterknife
    It worked for me.

    0 讨论(0)
  • 2020-12-24 05:48

    In Kotlin, actually there is no need (or) necessity for going ButterKnife concepts. because in your activity you can directly refer the view _id of the layout file as referred below.

    layout.xml

    <Button
         android:id="@+id/btn_prestage"
         android:layout_width="20dp"
         android:layout_height="20dp"
         android:background="@drawable/prestaging_off"/>
    

    Activity.kt

     btn_prestage.setBackgroundResource(R.drawable.staging_on)
     btn_prestage.setOnClickListener{ view ->
                Snackbar.make(view, "My Action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show() }
    

    build.gradle(app)

    apply plugin: 'com.android.application'
    apply plugin: 'kotlin-android'
    apply plugin: 'kotlin-android-extensions'
    apply plugin: 'kotlin-kapt'
    
    android {
       dependencies {... }
    }
    
    kapt {
        generateStubs = true
    }
    
    0 讨论(0)
  • 2020-12-24 05:49

    There's no need for butterknife in Kotlin. You can directly use the following:

    // app:build.gradle file

    apply plugin: 'com.android.application'
    apply plugin: 'kotlin-android'
    apply plugin: 'kotlin-android-extensions'
    apply plugin: 'kotlin-kapt'
    
    android {
        compileSdkVersion 26
        buildToolsVersion "26.0.1"
        defaultConfig {
            applicationId "com.example.nikhiljadhav.myapplication"
            minSdkVersion 15
            targetSdkVersion 26
            versionCode 1
            versionName "1.0"
            testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
    }
    
    dependencies {
        implementation fileTree(include: ['*.jar'], dir: 'libs')
        implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
        implementation 'com.android.support:appcompat-v7:26.0.0'
        implementation 'com.android.support.constraint:constraint-layout:1.0.2'
        implementation 'com.android.support:design:26.0.0'
        testImplementation 'junit:junit:4.12'
        androidTestImplementation 'com.android.support.test:runner:1.0.0'
        androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.0'
    }
    
    kapt {
        generateStubs = true
    }
    

    // xml layout file

    <TextView
        android:id="@+id/tvHello"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        android:layout_marginEnd="8dp"
        app:layout_constraintStart_toStartOf="parent"
        android:layout_marginStart="8dp" />
    
    <TextView
        android:id="@+id/tvId"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        android:layout_marginEnd="8dp"
        app:layout_constraintStart_toStartOf="parent"
        android:layout_marginStart="8dp" />
    
    <EditText
        android:id="@+id/etDemo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        android:layout_marginEnd="8dp"
        android:onClick="onClick"
        app:layout_constraintStart_toStartOf="parent"
        android:layout_marginStart="8dp" />
    

    // MainActivity.kt file

    class MainActivity : AppCompatActivity() {
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
            setSupportActionBar(toolbar)
    
            // use the kotlin property
            tvHello.text="Hi bla bla"
            tvId.text="buubububub"
            //set textcolor  
            tvId.setTextColor(ContextCompat.getColor(this, R.color.colorAccent)) 
            etDemo.hint="nhdodfhfgf"
    
            tvId.setOnClickListener{ view->
                onClick(view)
            }
    
            fab.setOnClickListener { view ->
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show()
            }
        }
    
        fun onClick(view: View) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show()
        }
    
        ...
    }
    

    For onTextChangeListner:

    etText.addTextChangedListener(object : TextWatcher{
            override fun afterTextChanged(p0: Editable?) {
                TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
            }
    
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
                TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
            }
    
            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
                TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
            }
    
        }) 
    
    0 讨论(0)
提交回复
热议问题