æ¦è¦
Android ã¢ããªéçºã®åå¼·ã§ã¦ã¼ã¶ã¼æä½ãããªã¬ã¼ã«ä»»æã®å¦çãè¡ããªã¹ãã¼ã®ä½¿ãæ¹ãåå¼·ããã®ã§ãã®åå¿é²ã§ãã
ååä½æãããµã³ãã«ç»é¢ã«å¯¾ãã¦ããªã¹ãã¼ã追å ãã¦ããã¾ãã
ããã°è¨äºç·¨é - ã¯ã¦ãªããã° (hatena.ne.jp)
使ç¨ç°å¢
- WIndows 11
- Android Studio Giraffe | 2022.3.1 Patch 3
- Kotlin
å 容
ã¾ãã¯ãªã¹ãã¼ã«ã¤ãã¦ã
ã¦ã¼ã¶ã¼ããã¿ã³ã¿ãããããªã©ã®ã¢ã¯ã·ã§ã³ãè¡ãããããã¤ãã³ãã¨å¼ã¶ã
ãã®ã¤ãã³ãã«å¯¾å¿ããå¦çã®ãã¨ããã¤ãã³ããã³ãã©ã¨å¼ã¶ãï¼ä¾ï¼ãã¿ã³ã¿ãããããããã©ã¼ã ã«å ¥åãããå 容ãéä¿¡ããï¼
ããã¦ãä¸è¨ã®ãããªã¦ã¼ã¶ã¼ã®ãã¿ã³ã¿ããã¤ãã³ããæ¤ç¥ãããã®ãããªã¹ãã¨å¼ã¶ã
Â
ä¸è¨ã®ãããªæµãã¨ãªãããã§ãã
- ã¦ã¼ã¶ã¼ã«ãããã¿ã³ã¿ããï¼ã¤ãã³ãï¼
- ãªã¹ãããã¿ã³ã¿ããã¤ãã³ããæ¤ç¥ï¼ãªã¹ãï¼
- ãã¿ã³ã¤ãã³ãã«å¯¾å¿ããã¤ãã³ããã³ãã©ãå®è¡ï¼ã¤ãã³ããã³ãã©ï¼
Â
ä¸è¨ã§ã¯ä¸è¨ã®æµããè¡ããããã«ãµã³ãã«ç»é¢ãä¿®æ£ãã¦ããã¾ãã
Â
æé ï¼ï¼ç»é¢é¨åãç¨æ
activity_main.xml ãä¸è¨å 容ã«å¤æ´ã
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:orientation="vertical">
<TextView
android:id="@+id/HelloTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:text="@string/hello_title"/>
<EditText
android:id="@+id/EditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:inputType="text"/>
<Button
android:id="@+id/btClick"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="@string/hello_button"/>
<TextView
android:id="@+id/HelloOutput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"/>
</LinearLayout>
Â
ãã®ç¶æ ã§ã®ç»é¢è¡¨ç¤ºå 容ã¯ä¸è¨ã®ããã«ãªã£ã¦ããã
ãã®å¾ã®æé ã§ãããã¹ããã£ã¼ã«ãã«ä»»æã®å 容ãå ¥åããHello ãã¿ã³ãã¿ããããã ãHello ãã¿ã³ä¸é¨ã«å ¥åå 容ã表示ãããããã«ãªã¹ããè¨å®ããã
Â
Â
æé ï¼ï¼ãªã¹ããå®ç¾©
ãããã㯠MainActivity.kt ãç·¨éãã¦ããã
ã¡ãªã¿ã«ãMainActivity.kt ã¯åæç¶æ ã§ã¯ä¸è¨ã®å 容ã¨ãªã£ã¦ããã
package com.example.samplescreen
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}
Â
setContentView(R.layout.activity_main)
âã®é¨å㧠activity_main.xml ãèªã¿è¾¼ãã§ãããã¨ãããã£ãã
ãªããonCreate 㯠MainActivity ãèªã¿è¾¼ã¾ããã¨ãã«å®è¡ãããå¦çã§ãããã«ãªã¹ãã追å ãã¦ããã
Â
MainActivity å ã«ä»¥ä¸ã®ãããªãªã¹ããå®ç¾©ã
View.OnclickListener ãä»å使ç¨ãããªã¹ãã§ãViewã«å®è¡ãããã¤ãã³ãå¦çã«å¿ãããªã¹ãã¤ã³ã¿ã¼ãã§ã¤ã¹ãç¨æããã¦ãããã¤ã³ã¿ã¼ãã§ã¤ã¹ãç¶æ¿ããå½¢ã§ãªã¹ããå®è£ ããã
Â
private inner class ButtonClickListener : View.OnClickListener {
@SuppressLint("SetTextI18n")
override fun onClick(v: View?) {
// å ¥åã»åºå対象ã®ç»é¢é¨åãåå¾
val input = findViewById<EditText>(R.id.EditText)
val output = findViewById<TextView>(R.id.HelloOutput)
// å ¥åå¤ãèªã¿è¾¼ã
val inputText = input.text.toString()
// å ¥åå¤ãç»é¢ã«åæ
output.text = "å ¥åå¤ï¼$inputText"
}
}
Â
ããã¦æå¾ã«ãonCreate å ã«ãªã¹ããè¨å®ããå¦çã追å ããã
Â
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val btClickButton = findViewById<Button>(R.id.btClick)
// ãªã¹ããã¤ã³ã¹ã¿ã³ã¹å
val listener = ButtonClickListener()
// ãªã¹ããè¨å®
btClickButton.setOnClickListener(listener)
}
Â
æé ï¼ï¼ã¨ãã¥ã¬ã¼ã¿ã¼ãç«ã¡ä¸ããã¢ããªèµ·å
ããã¹ããã£ã¼ã«ãã«ä»»æã®æååãå ¥åããHello ãã¿ã³ãã¿ããããã¨ã
以ä¸ã®ããã«å ¥åããå 容ãç»é¢ã«è¡¨ç¤ºãããã
Â
Â
åè
- åºç¤ï¼å¿ç¨åããã£ããè²æï¼Androidã¢ããªéçºã®æç§æ¸ 第3ç Kotlinå¯¾å¿ ãªãã¡ãã£ã¦éçºè ã«ãªããªãããã®å®è·µãã³ãºãªã³ Kindleç
- å ¥åã¤ãã³ãã®æ¦è¦  | Android ãããããã¼ Â | Android Developers (google.cn)
Â