この記事は2020年08月11日に投稿しました。
目次
リンク
1. はじめに
こんにちは、iOSのエディタアプリPWEditorの開発者の二俣です。
今回は業務で使用しているAndroidで画面遷移する方法についてです。
2. Androidで画面遷移する
Androidで画面遷移するには、以下のような実装になります。
実装例
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="match_parent"> <Button android:id="@+id/subButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="サブ画面"/> </LinearLayout>
activity_sub.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="match_parent"> <Button android:id="@+id/mainButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="メイン画面"/> </LinearLayout>
MainActivity.kt
package info.paveway.samplescreentransition import android.content.Intent import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.view.View import android.widget.Button class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // サブ画面ボタンにリスナーを設定します。 val listener = SubButtonOnClickListener() val button = findViewById<Button>(R.id.subButton) button.setOnClickListener(listener) } // サブ画面ボタンがクリックされた時に呼び出されるリスナークラス private inner class SubButtonOnClickListener : View.OnClickListener { override fun onClick(view: View?) { // サブ画面を表示します。 val intent = Intent(applicationContext, SubActivity::class.java) startActivity(intent) } } }
SubActivity.kt
package info.paveway.samplescreentransition import android.os.Bundle import android.view.View import android.widget.Button import androidx.appcompat.app.AppCompatActivity class SubActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_sub) // メイン画面ボタンにリスナーを設定します。 val listener = MainButtonOnClickListener() val button = findViewById<Button>(R.id.mainButton) button.setOnClickListener(listener) } // メイン画面ボタンがクリックされた時に呼び出されるリスナークラス private inner class MainButtonOnClickListener : View.OnClickListener { override fun onClick(view: View?) { // この画面を終了する。 finish() } } }
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="info.paveway.samplescreentransition"> <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> <!-- サブ画面の定義を追加します。 --> <activity android:name=".SubActivity"/> </application> </manifest>
実行結果
メイン画面
サブ画面
3. おわりに
今回はただ単にメイン画面からサブ画面、サブ画面からメイン画面に画面遷移する方法を紹介しました。
リンク
紹介している一部の記事のコードはGitlabで公開しています。
興味のある方は覗いてみてください。
私が勤務しているニューラルでは、主に組み込み系ソフトの開発を行っております。
弊社製品のハイブリッドOS [Bi-OS][Bi-OS]は高い技術力を評価されており、特に制御系や通信系を得意としています。
私自身はiOSモバイルアプリやウィンドウズアプリを得意としております。
ソフトウェア開発に関して相談などございましたら、お気軽にご連絡ください。
また一緒に働きたい技術者の方も随時募集中です。
興味がありましたらご連絡ください。
EMAIL : info-nr@newral.co.jp / m-futamata@newral.co.jp
TEL : 042-523-3663
FAX : 042-540-1688