Я'м пытаясь использовать Котлин с Butterknife для моего приложения для Android.
Вот моя сборка.Gradle в
dependencies {
...
compile 'com.jakewharton:butterknife:8.0.1'
kapt 'com.jakewharton:butterknife-compiler:8.0.1'
}
kapt {
generateStubs = true
}
У меня тоже есть EditText и я хочу показать сообщение, используя ButterKnife, когда это изменилось:
@OnTextChanged(R.id.input)
fun test() {
toast(1)
}
Однако, ничего не происходит. Я поставил точку останова в функцию - и это даже не казнили.
P. S: Я слышал о kotterknife, однако я видел это пример с чисто Butterknife.
Что я делаю не так?
Там's нет необходимости для butterknife в Котлин. Вы можете напрямую использовать следующие:
// приложение:построить.файл Gradle
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
<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.файл КТ
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()
}
...
}
Для 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.
}
})
В вашем уровне приложения сборки.Gradle в
apply plugin: 'kotlin-android'
kapt {
generateStubs = true
}
dependencies {
compile 'com.jakewharton:butterknife:8.4.0'
kapt 'com.jakewharton:butterknife-compiler:8.4.0'
}
В своем топ-уровне строить.Gradle в
buildscript {
ext.kotlin_version = '1.1.3'
repositories {
jcenter()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
Активность
@BindView(R.id.toolbar) @JvmField var toolbar: Toolbar? = null
или
@BindView(R.id.toolbar) lateinit var toolbar: Toolbar
Внутри Метод
ButterKnife.bind(this)
Создатели Котлин сказать на их сайте, что: Плагин Котлин дополнения Android (автоматически входит в плагин Котлин в Андроид студии) решает тот же вопрос: Рзаменено `findViewById, чтобы найти с лаконичной и простой код. Рассмотрите возможность использования его если вы're уже через ButterKnife и Дон'т хотите перенести.
и, например,
// Using R.layout.activity_main from the main source set
import kotlinx.android.synthetic.main.activity_main.*
class MyActivity : Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
textView.setText("Hello, world!")
// Instead of findViewById(R.id.textView) as TextView
}
}
поле TextView
является расширением собственность для "активности", и он имеет тот же тип, что объявлен в activity_main.xml`.
В Gradle в:
compile 'com.jakewharton:butterknife:8.8.0'
kapt "com.jakewharton:butterknife-compiler:8.8.0"
В своей деятельности
@BindView(R.id.toolbar)
lateinit var mToolbar: Toolbar
Конечно, помню ButterKnife.привязать(это) и применить плагин на верхней части вашего приложения.применить плагин Gradle в: 'Котлин-kapt'
Полная ссылка: https://github.com/JetBrains/kotlin-examples/tree/master/gradle/android-butterknife
Вам нужно просто добавить `ButterKnife.КТ в вашем исходном дереве по следующей ссылке: https://github.com/JakeWharton/kotterknife Он работал для меня.
вы можете импортировать все синтетические свойства макета free/res/layout/activity_main.xml путем добавления этого импорт:
import kotlinx.android.synthetic.main.activity_main.*
Теперь вы можете открыть все представления с использованием ИД's нет необходимости, чтобы начать findbyid осуществляет
Вы можете реализовать некоторые расширения, чтобы улучшить свое поведение, взгляды. Оформить этот пример с "onTextChange" в регулярном полей EditText:
fun EditText.onTextChange(callback: (text: CharSequence?, start: Int, before: Int, count: Int) -> Unit) {
addTextChangedListener(object : TextWatcher {
override fun afterTextChanged(s: Editable?) {}
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
callback(s, start, before, count)
}
})
}
Использование:
m_editText.onTextChange { text, _, _, _ ->
m_textView.text = text
}
Я голосую за Котлин-андроид-расширения
Добавьте это в ваш проект построить.Gradle в
buildscript {
ext.kotlin_version = '1.1.2-4'
ext.butterknife_version = '8.6.0'
repositories {
maven { url 'https://maven.google.com' }
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.0-alpha1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "com.jakewharton:butterknife-gradle-plugin:$butterknife_version"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
И в ваше приложение.Добавить Gradle в этом.
//Butterknife
compile "com.jakewharton:butterknife:$butterknife_version"
kapt "com.jakewharton:butterknife-compiler:$butterknife_version"
Джейк Уортон создал новую библиотеку для Котлин назвали kotterknife: https://github.com/JakeWharton/kotterknife Gradle в:
compile 'com.jakewharton:kotterknife:0.1.0-SNAPSHOT'
Вид:
val lastName: TextView by bindView(R.id.last_name)
// Optional binding.
val details: TextView? by bindOptionalView(R.id.details)
// List binding.
val nameViews: List<TextView> by bindViews(R.id.first_name, R.id.last_name)
// List binding with optional items being omitted.
val nameViews: List<TextView> by bindOptionalViews(R.id.first_name, R.id.middle_name, R.id.last_name)
В Котлин, на самом деле нет нужды (или) необходимости идти концепций ButterKnife. потому что в вашей деятельности вы можете напрямую обратиться в вид поля _id файла макета, как указано ниже.
layout.xml
<Button
android:id="@+id/btn_prestage"
android:layout_width="20dp"
android:layout_height="20dp"
android:background="@drawable/prestaging_off"/>
Деятельности.КТ
btn_prestage.setBackgroundResource(R.drawable.staging_on)
btn_prestage.setOnClickListener{ view ->
Snackbar.make(view, "My Action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show() }
сборки.Gradle в(приложение)
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
android {
dependencies {... }
}
kapt {
generateStubs = true
}