When using Kotlin on Android etc., there are many people who automatically convert from Java with "Convert Java File to Kotlin File". It actually works, but it's a memo to take it one step further and make better use of Kotlin.
2nd time This time, you will learn the dynamic processing of View of Android Activity. Kotlin version: 1.3
First, prepare a sample written in Java. The processing is.
MainActivity.java
public class MainActivity extends AppCompatActivity {
Button buttonA;
TextView textViewA;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonA = (Button) findViewById(R.id.button_A);
buttonA.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("", "buttonA");
}
});
textViewA = (TextView) findViewById(R.id.text_viewA);
textViewA.setText(R.string.text1);
TextView textViewB = (TextView) findViewById(R.id.text_viewB);
textViewB.setText(R.string.text2);
}
@Override
protected void onRestart() {
super.onRestart();
buttonA.setClickable(false);
textViewA.setText(R.string.text2);
}
If this is automatically converted and matched to the original shape, it will surely look like this ...
MainActivity.kt
class MainActivity : AppCompatActivity() {
var buttonA: Button? = null
var textViewA: TextView? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
buttonA = findViewById<View>(R.id.button_A) as Button
buttonA?.setOnClickListener { Log.d("", "buttonA") }
textViewA = findViewById<View>(R.id.text_viewA) as TextView
textViewA?.setText(R.string.text1)
val textViewB = findViewById<View>(R.id.text_viewB) as TextView
textViewB.setText(R.string.text2)
}
override fun onRestart() {
super.onRestart()
buttonA?.isClickable = false
textViewA?.setText(R.string.text2)
}
}
For the time being, it can work without problems ... First, you have to unwrap Nullable (buttonA? ....) every time you use a member variable. findViewById as ***** The description is also meaningless.
There are two ways
MainActivity.kt
class MainActivity : AppCompatActivity() {
lateinit var buttonA: Button
lateinit var textViewA: TextView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
buttonA = findViewById<Button>(R.id.button_A)
buttonA.setOnClickListener { Log.d("", "buttonA") }
textViewA = findViewById<TextView>(R.id.text_viewA)
textViewA.setText(R.string.text1)
val textViewB = findViewById<TextView>(R.id.text_viewB)
textViewB.setText(R.string.text2)
}
override fun onRestart() {
super.onRestart()
buttonA.isClickable = false
textViewA.setText(R.string.text2)
}
}
It is necessary to add classpath to gradle to use
build.gradle
buildscript {
ext.kotlin_version = '1.3.41'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
//Add the following line
classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version"
}
}
Now ready Can be implemented without using findViewById
MainActivity.kt
//* Additional import of ↓ is required
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
button_A.setOnClickListener { Log.d("", "buttonA") }
text_viewA.text = getText(R.string.text1)
text_viewB.text = getText(R.string.text2)
}
override fun onRestart() {
super.onRestart()
button_A.isClickable = false
text_viewA.text = getText(R.string.text2)
}
}
Very simple!
Recommended Posts