「Android/kotlin/helloworld」の版間の差分
提供: 初心者エンジニアの簡易メモ
(→helloworld) |
|||
| 行6: | 行6: | ||
setContentView(R.layout.activity_main) | setContentView(R.layout.activity_main) | ||
text1.text = "hello"; // helloで上書き | text1.text = "hello"; // helloで上書き | ||
| − | |||
} | } | ||
} | } | ||
| 行31: | 行30: | ||
</androidx.constraintlayout.widget.ConstraintLayout> | </androidx.constraintlayout.widget.ConstraintLayout> | ||
</pre> | </pre> | ||
| + | |||
| + | ==javaっぽく、こんな感じでも一応書ける== | ||
| + | 使わないほうが良いが・・。 | ||
| + | val text = findViewById(R.id.text1) as TextView | ||
| + | text.setText("hogehoge") | ||
2020年2月13日 (木) 15:14時点における版
helloworld
MainActivity.kt
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
text1.text = "hello"; // helloで上書き
}
}
res/layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".MainActivity">
<TextView
android:id="@+id/text1"
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" />
</androidx.constraintlayout.widget.ConstraintLayout>
javaっぽく、こんな感じでも一応書ける
使わないほうが良いが・・。
val text = findViewById(R.id.text1) as TextView
text.setText("hogehoge")
