安卓开始
2023年10月17日调试
Log.e:表示错误信息,比如可能导致程序崩溃的异常。
Log.w:表示警告信息。
Log.i:表示一般消息。
Log.d:表示调试信息,可把程序运行时的变量值打印出来,方便跟踪调试。
Log.v:表示冗余信息。
目录常用结构
res子目录,存放当前模块的资源文件。res下面又有4个子目录:
drawable目录存放图形描述文件与图片文件。
layout目录存放App页面的布局文件。
mipmap目录存放App的启动图标。
values目录存放一些常量定义文件,例如字符串常量strings.xml、像素常量dimens.xml、颜色常量colors.xml、样式风格定义styles.xml等。
Gradle Scripts下面主要是工程的编译配置文件,主要有:
(1)build.gradle,该文件分为项目级与模块级两种,用于描述App工程的编译规则。
(2)proguard-rules.pro,该文件用于描述Java代码的混淆规则。
(3)gradle.properties,该文件用于配置编译工程的命令行参数,一般无须改动。
(4)settings.gradle,该文件配置了需要编译哪些模块。初始内容为include ‘:app’,表示只编译app模块。
(5)local.properties,项目的本地配置文件,它在工程编译时自动生成,用于描述开发者电脑的环境配置,包括SDK的本地路径、NDK的本地路径等。
编辑配置文件
模块配置文件详解
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
plugins { id("com.android.application") } android { namespace = "com.example.myapplication" compileSdk = 34 // 指定编译用的SDK版本号。比如30表示使用Android 11.0编译 defaultConfig { applicationId = "com.example.myapplication" // 指定该模块的应用编号,也就是App的包名 minSdk = 28 // 指定App适合运行的最小SDK版本号。比如19表示至少要在Android 4.4上运行 targetSdk = 33 // 指定目标设备的SDK版本号。表示App最希望在哪个版本的Android上运行 versionCode = 1 // 指定App的应用版本号 versionName = "1.0" // 指定App的应用版本名称 testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" } buildTypes { release { isMinifyEnabled = false proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro") } } compileOptions { sourceCompatibility = JavaVersion.VERSION_1_8 targetCompatibility = JavaVersion.VERSION_1_8 } } dependencies { implementation("androidx.appcompat:appcompat:1.6.1") implementation("com.google.android.material:material:1.10.0") implementation("androidx.constraintlayout:constraintlayout:2.1.4") testImplementation("junit:junit:4.13.2") // 指定单元测试编译用的junit版本号 androidTestImplementation("androidx.test.ext:junit:1.1.5") androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1") } |
安卓视图入口配置文件详解
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"> <!-- android:allowBackup,是否允许应用备份。允许用户备份系统应用和第三方应用的apk安装包和应用数据,以便在刷机或者数据丢失后恢复应用,用户即可通过adb backup和adb restore来进行对应用数据的备份和恢复。为true表示允许,为false则表示不允许。 android:icon,指定App在手机屏幕上显示的图标。 android:label,指定App在手机屏幕上显示的名称。 android:roundIcon,指定App的圆角图标。 android:supportsRtl,是否支持阿拉伯语/波斯语这种从右往左的文字排列顺序。为true表示支持,为false则表示不支持。 android:theme,指定App的显示风格。 --> <application android:allowBackup="true" android:dataExtractionRules="@xml/data_extraction_rules" android:fullBackupContent="@xml/backup_rules" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/Theme.MyApplication" tools:targetApi="31"> <!-- activity节点指定了该App拥有的活动页面信息,其中拥有 android.intent.action.MAIN的activity说明它是入口页面 --> <activity android:name=".MainActivity" android:exported="true"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> |
页面跳转案例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
package com.example.myapplication; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; /** * implements 扩展实现OnClickListener方法 */ public class MainActivity extends AppCompatActivity implements View.OnClickListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button button1 = findViewById(R.id.button1); //获取id button1.setOnClickListener(this); //给按钮添加时间将这个类传入进去 } /** * 实现的点击事件 * @param v The view that was clicked. */ public void onClick(View v) { startActivity(new Intent(this, MainActivity2.class)); } } |