Android の注意事項 (1): Android アクティビティ コンポーネントの作成と構成

1. アクティビティコンポーネントの概要

Activity コンポーネントは Android の 4 つの基本コンポーネントの 1 つであり、その主な機能はインターフェイスを定義することです。ほとんどのアプリには複数の画面が含まれています。つまり、アプリには複数のアクティビティが含まれています。通常、アプリケーション内のアクティビティはメイン アクティビティとして指定されます。メイン アクティビティは、ユーザーがアプリケーションを起動したときに表示される最初の画面です。各アクティビティは、別のアクティビティを開始して、さまざまな操作を実行できます。アクティビティは、アプリケーションがユーザーと対話するためのエントリ ポイントとして機能します。アクティビティは、アプリケーションがインターフェイスを描画するためのウィンドウを提供します。アクティビティはアプリケーションに画面を実装します。

2. アクティビティの作成と構成

Activity の主な機能は、モバイル アプリケーションのインターフェイスをカスタマイズすることです。現在、アクティビティのインターフェイスを定義するには主に 2 つの方法があります: (1) XML レイアウト ファイルを組み合わせてアクティビティのインターフェイスを定義する従来の方法、(2) Google が推進する Compose ツールキットでアクティビティのインターフェイスを定義します。

2.1 従来の組み合わせでは、XML レイアウト ファイルを使用してアクティブ インターフェイスを定義します

この開発ツールは「Android Studio Giraffe | 2022.3.1 Patch 1」を使用して新しいプロジェクト モジュールを作成するため、新しい Android プロジェクトを作成します。デフォルトのメイン アクティビティ MainActivity は ComponentActivity のサブクラスであり、Compose をサポートするインターフェイス プレゼンテーション メソッドを使用します。元の MainActivity.kt を削除します。
したがって、従来の XML レイアウト ファイルを組み合わせたアクティビティを作成するには、図 1 に示すように、マウスを右クリックし、ポップアップ 3 レベル メニューで [新規] -> [アクティビティ] -> [基本ビュー アクティビティ] を選択します。
ここに画像の説明を挿入します
このとき、AppCompatActivity クラスに対応する MainActivity などのサブアクティビティ クラスが生成されます。コードは次のとおりです。

class MainActivity : AppCompatActivity() {
    
         
	override fun onCreate(savedInstanceState: Bundle?) {
    
             
		super.onCreate(savedInstanceState)          
		setContentView(R.layout.activity_main)         
 }
}

以下に示すように、MainActivity に対応するレイアウト ファイルをカスタマイズします。

<?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"     
     android:background="@color/design_default_color_secondary"     
     tools:context=".MainActivity">      
     <TextView         
          android:id="@+id/textView"         
          android:layout_width="wrap_content"         
          android:layout_height="wrap_content"         
          android:text="Hello World"         
          android:textSize="30sp"         
          app:layout_constraintBottom_toBottomOf="parent"           
          app:layout_constraintEnd_toEndOf="parent"         
          app:layout_constraintStart_toStartOf="parent"         
          app:layout_constraintTop_toTopOf="parent" />  
</androidx.constraintlayout.widget.ConstraintLayout> 

アクティビティを定義した後、AndroidManifest.xml ファイルでアクティビティを構成する必要があります。特に注目すべきはスタイルの設定です。これは、
最新の Android Studio がデフォルトで Android JetPack Compose コンポーネントをサポートしているため、デフォルトのスタイル定義形式が一致しないためです。設定ファイルを次のように変更します。

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android">      
<application         
……  <!-- 省略 -->      
android:theme="@style/Theme.AppCompat">          
<activity             
	android:name=".MainActivity"             
	android:exported="true"             
	android:label="@string/app_name"             
	android:theme="@style/Theme.AppCompat">             
	<intent-filter>                 
		<action android:name="android.intent.action.MAIN" />                 
		<category android:name="android.intent.category.LAUNCHER" />             
	</intent-filter>         
</activity>     
</application> 
</manifest>

注: 上記のテーマはすべて、デフォルトのスタイルを次のように変更します。

android:theme = “@style/Theme.AppCompat”

AppCompatActvity サブクラスとの一貫性を表します。
バインドされた XML ファイルを処理する必要がある場合は、XML 構成の GUI コントロールを取得する必要があります。具体的な処理方法としては、findViewById のデフォルトの組み合わせで GUI コントロールにアクセスする方法と、ViewBinding を使用して GUI
コントロールにアクセスする方法の 2 つがあります。
(1) GUI コントロールへのアクセスには、findViewById のデフォルトの組み合わせが使用されます。

class MainActivity : AppCompatActivity() {
    
         
	override fun onCreate(savedInstanceState: Bundle?) {
    
             
	super.onCreate(savedInstanceState)          
      
	setContentView(R.layout.activity_main)         //必须先调用,否则检索控件失败
	val textView  = findViewById<TextView>(R.id.textView);        
 	textView.setText("Hello Android World")     }      
 }
 }

(2) viewBinding を使用して GUI コントロールにアクセスします。viewBinding
の使用を許可するには、これをプロジェクト モジュールの build.gradle で次の形式で設定する必要があります。

android { buildFeature{ viewBinding true } }




このとき、アクティビティ MainActivity を変更します。コードは次のとおりです。

class MainActivity : AppCompatActivity() {
    
         
	override fun onCreate(savedInstanceState: Bundle?) {
    
             
		super.onCreate(savedInstanceState)                
		val binding = ActivityMainBinding.inflate(layoutInflater)         
		binding.textView.text = "Hello Android World"        
		setContentView(binding.root)     
 	} 
}

実行結果を図 2 に示します。

ここに画像の説明を挿入します

2.2 JetPack Compose コンポーネントを使用してインターフェイスを定義する

(1) 新しいプロジェクトモジュールを作成します。
(2) 以下に示すように、モジュールの build.gradle を変更し、compose を使用する権限を追加します。

android{ buildFeature{ true を構成する} }




メイン アクティビティ MainActivity を定義します。コードは次のとおりです。

class MainActivity : ComponentActivity() {
    
         
override fun onCreate(savedInstanceState: Bundle?) {
    
             
	super.onCreate(savedInstanceState)         
	setContent {
    
                 
		Ch03DemoTheme {
    
                     
			Surface(                     
			modifier = Modifier.fillMaxSize(),                     
			color = Color(0x03,0xDA,0xC6)                 
			) {
    
                         
				Greeting("Android World")                
 					}            
 				}         
}     
} 
} 
@Composable fun Greeting(name: String, modifier: Modifier = Modifier) {
    
         
		Text(         
			text = "Hello $name!",         
			fontSize = 30.sp,         
			textAlign = TextAlign.Center,         
			modifier = modifier     
			)
 }

MainActivity も AndroidManifest.xml で設定する必要があります。

<application android:theme="@style/Theme.Ch03Demo">         
<activity             
	android:name=".MainActivity"            
	android:exported="true"             
	android:label="@string/app_name"             
	android:theme="@style/Theme.Ch03Demo">             
	<intent-filter>                 
		<action android:name="android.intent.action.MAIN" />                  
		<category android:name="android.intent.category.LAUNCHER" />             
	</intent-filter>         
</activity>     
</application>  
</manifest>

実行結果を図 3 に示します。
ここに画像の説明を挿入します
参照する:

「Jetpack Compose チュートリアル」 https://developer.android.google.cn/jetpack/compose/tutorial?hl=zh-cn

おすすめ

転載: blog.csdn.net/userhu2012/article/details/133280401