Android
 Computer >> コンピューター >  >> プログラミング >> Android

Kotlinを使用してAndroidのメインアクティビティにデータを送り返す方法は?


この例は、Kotlinを使用してAndroidのメインアクティビティにデータを送り返す方法を示しています

ステップ1 − Android Studioで新しいプロジェクトを作成し、[ファイル]⇒[新しいプロジェクト]に移動して、新しいプロジェクトを作成するために必要なすべての詳細を入力します。

ステップ2 −次のコードをres / layout/activity_main.xmlに追加します。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:gravity="center_horizontal"
   android:orientation="vertical"
   tools:context=".MainActivity">
   <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginTop="100dp"
      android:layout_marginBottom="50dp"
      android:text="Tutorials Point"
      android:textAlignment="center"
      android:textColor="@android:color/holo_green_dark"
      android:textSize="32sp"
      android:textStyle="bold" />
   <TextView
      android:id="@+id/textViewNumbers"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Numbers: "
      android:textColor="@android:color/black"
      android:textSize="24sp"
      android:textStyle="bold" />
   <Button
      android:id="@+id/buttonAdd"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginTop="10dp"
      android:text="add" />
   <Button
      android:id="@+id/buttonSubtract"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginTop="5dp"
      android:text="subtract" />
</LinearLayout>

ステップ3 −次のコードをsrc / MainActivity.kt

に追加します
import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
   private lateinit var textViewResult: TextView
   private lateinit var editTextNumber1: EditText
   private lateinit var editTextNumber2: EditText
   private lateinit var button: Button
   override fun onCreate(savedInstanceState: Bundle?) {
      super.onCreate(savedInstanceState)
      setContentView(R.layout.activity_main)
      title = "KotlinApp"
      textViewResult = findViewById(R.id.textViewResult)
      editTextNumber1 = findViewById(R.id.editTextNumber1)
      editTextNumber2 = findViewById(R.id.editTextNumber2)
      button = findViewById(R.id.btnOpenActivity2)
      button.setOnClickListener {
         if ((editTextNumber1.text.toString() == "" || editTextNumber2.text.toString() == "")) {
            Toast.makeText(this@MainActivity, "Please insert numbers", Toast.LENGTH_SHORT).show()
         } else {
            val number1 = Integer.parseInt(editTextNumber1.text.toString())
            val number2 = Integer.parseInt(editTextNumber2.text.toString())
            val intent = Intent(this@MainActivity, SecondActivity::class.java)
            intent.putExtra("number1", number1)
            intent.putExtra("number2", number2)
            startActivityForResult(intent, 1)
         }
      }
   }
   override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
      super.onActivityResult(requestCode, resultCode, data)
      if (requestCode == 1) {
         if (resultCode == Activity.RESULT_OK) {
            val result = data!!.getIntExtra("result", 0)
            textViewResult.text = "" + result
         }
         if (resultCode == Activity.RESULT_CANCELED) {
            textViewResult.text = "Nothing selected"
         }
      }
   }
}

ステップ4 −新しいアクティビティを作成し、次のコードを追加します−

activity_second.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:gravity="center_horizontal"
   android:orientation="vertical"
   tools:context=".MainActivity">
   <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginTop="100dp"
      android:layout_marginBottom="50dp"
      android:text="Tutorials Point"
      android:textAlignment="center"
      android:textColor="@android:color/holo_green_dark"
      android:textSize="32sp"
      android:textStyle="bold" />
   <TextView
      android:id="@+id/textViewNumbers"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Numbers: "
      android:textColor="@android:color/black"
      android:textSize="24sp"
      android:textStyle="bold" />
   <Button
      android:id="@+id/buttonAdd"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginTop="10dp"
      android:text="add" />
   <Button
      android:id="@+id/buttonSubtract"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginTop="5dp"
      android:text="subtract" />
</LinearLayout>

SecondActivity.kt

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
class SecondActivity : AppCompatActivity() {
   lateinit var textViewNumber: TextView
   lateinit var buttonAdd: Button
   lateinit var buttonSubtract: Button
   override fun onCreate(savedInstanceState: Bundle?) {
      super.onCreate(savedInstanceState)
      setContentView(R.layout.activity_second)
      val intent = intent
      val number1 = intent.getIntExtra("number1", 0)
      val number2 = intent.getIntExtra("number2", 0)
      textViewNumber = findViewById(R.id.textViewNumbers)
      textViewNumber.text = "Numbers: $number1, $number2"
      buttonAdd = findViewById(R.id.buttonAdd)
      buttonSubtract = findViewById(R.id.buttonSubtract)
      buttonAdd.setOnClickListener {
         val result = number1 + number2
         val resultIntent = Intent()
         resultIntent.putExtra("result", result)
         setResult(Activity.RESULT_OK, resultIntent)
         finish()
      }
      buttonSubtract.setOnClickListener {
         val result = number1 - number2
         val resultIntent = Intent()
         resultIntent.putExtra("result", result)
         setResult(Activity.RESULT_OK, resultIntent)
         finish()
      }
   }
}

ステップ5 −次のコードをandroidManifest.xmlに追加します

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.q11">
   <application
      android:allowBackup="true"
      android:icon="@mipmap/ic_launcher"
      android:label="@string/app_name"
      android:roundIcon="@mipmap/ic_launcher_round"
      android:supportsRtl="true"
      android:theme="@style/AppTheme">
      <activity android:name=".MainActivity">
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
      </activity>
   </application>
</manifest>

アプリケーションを実行してみましょう。実際のAndroidモバイルデバイスをコンピューターに接続していると思います。 Android Studioからアプリを実行するには、プロジェクトのアクティビティファイルの1つを開き、[実行]アイコンをクリックします ツールバーからKotlinを使用してAndroidのメインアクティビティにデータを送り返す方法は? 。オプションとしてモバイルデバイスを選択し、デフォルトの画面を表示するモバイルデバイスを確認します

Kotlinを使用してAndroidのメインアクティビティにデータを送り返す方法は?

Kotlinを使用してAndroidのメインアクティビティにデータを送り返す方法は?

Kotlinを使用してAndroidのメインアクティビティにデータを送り返す方法は?


  1. Androidで戻るボタンを押しても前のアクティビティに戻らないようにする方法

    Androidアプリ開発では、ユーザーが戻るボタンを押したときに、前の画面へ戻る動作を無効にしたい場面が数多くあります。例えば、ログイン後の画面や、特定の処理フロー中の画面などがその代表例です。 本記事では、戻るボタンの操作を無効化し、以前のアクティビティへ戻らないようにする方法を、サンプルコードとともに段階的に解説します。 実装手順 ステップ1:新しいプロジェクトを作成する Android Studioを起動し、メニューから「File」→「New Project」を選択します。必要な項目をすべて入力して、新しいプロジェクトを作成しましょう。 ステップ2:レイアウトファイルにコードを追加する

  2. 【Android】セカンドアクティビティからメインアクティビティへデータを返す方法を解説

    このチュートリアルでは、Androidアプリにおいてセカンドアクティビティからメインアクティビティへデータ(処理結果)を返す方法を、サンプルコードを交えて解説します。2つの数値を入力して別画面へ渡し、その画面での計算結果を元の画面に表示させるアプリを例に、データ受け渡しの基本を学びましょう。ポイントは startActivityForResult() と onActivityResult() の2つです。 ステップ1:新規プロジェクトを作成する Android Studioを起動し、「File」→「New Project」を選択して、必要な項目を入力して新しいプロジェクトを作成します。 ス