Android应用开发
日志信息
-
Log.e
:表示错误信息,比如可能导致程序崩溃的异常。 -
Log.w
:表示警告信息。 -
Log.i
:表示一般消息。 -
Log.d
:表示调试信息,可把程序运行时的变量值打印出来,方便跟踪调试。 -
Log.v
:表示冗余信息。
项目工程结构
清单文件文件:
app/src/main/AndroidManifests.xml
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/Theme.Demo01" 应用主题
控件
简单控件
TextView
<TextView android:id="@+id/tv" android:textColor="@color/tv_text_color" //字体颜色 android:textStyle="bold" //字体样式 android:textSize="30sp" //字体大小(一般使用sp作为单位) android:background="@color/tv_background_color" //背景色 android:layout_width="match_parent" //控件尺寸 android:layout_height="300dp" android:gravity="center_vertical" //字体分布方式 android:text="@string/tv_content" //文本 //文字跑马灯显示 android:singleLine="true" //文本单行显示 android:ellipsize="marquee" android:marqueeRepeatLimit="marquee_forever" android:focusable="true" android:focusableInTouchMode="true" android:clickable="true" //可点击 //文字的阴影 android:shadowColor="@color/red" android:shadowRadius="3.0" android:shadowDx="10.0" android:shadowDy="10.0"> <requestFocus /> //自动获取焦点 </TextView>
Button
<Button android:id="@+id/btn" android:layout_width="200dp" android:layout_height="100dp" android:background="@drawable/btn_selector" android:backgroundTint="@color/btn_color_selector" android:onClick="" //定义点击函数 android:text="@string/btn_content" />
Button btn = findViewById(R.id.btn); //绑定按钮点击回调事件函数 //单击事件 btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.d(TAG, "onClick"); } }); //长按事件 btn.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { Log.d(TAG, "onLongClick"); return false; //当长按按钮后如果返回值是false则会再调用onClick()方法,如果返回值是true则不会 } }); //触摸事件 btn.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { Log.d(TAG, "onTouch: " + event.getAction()); return false; } });
EditText
<EditText android:id="@+id/et" android:hint="请输入用户名" //提示文本 android:drawableLeft="@drawable/baseline_person_24" //左侧icon android:drawablePadding="10dp" //与左侧icon的距离 android:background="#00ffffff" //背景色(透明) android:textColorHint="#AFB8BF" //提示文本的颜色 android:inputType="number" //输入文本的类型(text|textPassword) android:layout_width="250dp" android:layout_height="80dp" />
ImageView
<ImageView android:src="@drawable/test1" android:scaleType="matrix" android:layout_width="200dp" android:layout_height="200dp" /> <ImageView android:maxHeight="200dp" //设置ImageView组件的最大长宽,并自动拟合图片的尺寸 android:maxWidth="200dp" android:adjustViewBounds="true" android:src="@drawable/test1" //图片资源名称(放在drawble文件夹中) android:layout_width="wrap_content" android:layout_height="wrap_content" />
android:scaleType
的取值:
fitXY
:拉伸填充fitCenter
:等比例放缩至长短边契合
ProgressBar
圆形进度条
<ProgressBar android:id="@+id/pb" android:layout_width="wrap_content" android:layout_height="wrap_content" />
pb.setVisibility(View.VISIBLE); //设置进度条的可见性(可见) pb.setVisibility(View.GONE); //设置进度条的可见性(不可见)
条形进度条
<!-- 长条形进度 --> <ProgressBar android:id="@+id/pb2" style="?android:attr/progressBarStyleHorizontal" //设置样式为长条形进度条 android:max="100" //进度条对应的最大值 android:layout_width="300dp" //进度条的长度 android:layout_height="wrap_content" />
int progress = pb2.getProgress(); //获取当前进度数值 pb2.setProgress(); //设置当前进度数值
Notification
软件消息通知
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); //创建通知管理对象 NotificationChannel channel = new NotificationChannel("max", "测试通知", NotificationManager.IMPORTANCE_HIGH); //设置通知的重要性(一般都是`NotificationManager.IMPORTANCE_HIGH`) manager.createNotificationChannel(channel); //设置跳转 Intent intent = new Intent(this, NotificationActivity2.class); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_MUTABLE); notification = new NotificationCompat .Builder(this, "max") .setContentTitle("通知标题") //设置通知标题 .setContentText("通知中的内容:xxxxx") //设置通知内容 .setSmallIcon(R.drawable.baseline_person_24) //设置小图标的显示 .setColor(Color.parseColor("#ff0000")) //设置小图标的颜色(无效) .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.test1)) //设置大图片的显示 .setContentIntent(pendingIntent) //设置点击通知后跳转的页面 .setAutoCancel(true) .build();
manager.notify(1, notification); //发送通知 manager.cancel(1); //取消通知
Toolbar
<Toolbar android:background="#ff0000" //背景色 android:navigationIcon="@drawable/baseline_arrow_back_24" android:title="标题名" //标题名 android:subtitle="子标题名" android:logo="@drawable/ic_launcher_foreground" android:titleMarginStart="90dp" android:titleTextColor="#0000ff" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" />
字体大小
单位
px: 它是手机屏幕的最小显示单位,与设备的显示屏有关。
dp: 它是与设备无关的显示单位,只与屏幕的尺寸有关。
sp: 它专门用来设置字体大小,在系统设置中可以调整字体大小。(常使用这个单位)
为了适配不同的屏幕尺寸,字体大小使用:
sp
,控件尺寸使用:dp
density
:像素密度(1个dp对应几个px)
dpi
:每英寸距离中的像素数量(通过求屏幕对 角线上的像素数量除以屏幕尺寸)
dip
:
使用代码设置
在代码中获取View控件
View view = findViewById(R.id.xxx)
view.setText(xxx)
:设置文本内容
view.setTextColor(Color.xxx)
:设置文本颜色
view.setBackgroundColor(Color.xxx)
:设置背景颜色
TextView
,Button
,ImageView
等控件都是继承自View
在代码中获取布局组件
ViewGroup.LayoutParams params = view.getLayoutParams(); //获取布局组件 params.width = xxx; //设置视图的宽度 params.height = xxx; //设置视图的高度
//获取屏幕的像素密度从而进行单位转换 public class Utils { public static int dp2px(Context cxt, float dpValue) { float scale = cxt.getResources().getDisplayMetrics().density; return Math.round(dpValue * scale); } }
Kotlin语法
输入/输出
print()
println()
:末尾有一个换行符
变量类型
变量声明
-
只能读的变量(read-only variable)需要使用关键字
val
,只能被赋值1次val a: Int = 1 // 直接赋值 val b = 2 // 自动推测出是Int类型 val c: Int // 当没有赋初始值时,需要声明类型 c = 3 // 这里是推迟赋值 -
可以多次赋值的变量用关键字
var
var x = 5 // 自动推测出是Int型 x += 1 val PI = 3.14 var x = 0 fun incrementX() { x += 1 }
基本数据类型
Category | Basic types |
---|---|
Integers | Byte , Short , Int , Long |
Unsigned integers | UByte , UShort , UInt , ULong |
Floating-point numbers | Float , Double |
Booleans | Boolean |
Characters | Char |
Strings | String |
String类型:
-
字符串格式化(模板字符串)
“xxx$var1xxx${some_expression}”
集合
-
List
val li = listOf[<Type>](item1, item2, ...)
:声明只读数组val li = Arrays.asList(item1, item2, ...)
-
Map
val mp = mapOf(k1 to v1, k2 to v2, xxx to xxx, ...)
遍历范围:
for (i in 1..10) { //[1, 10] } for (i in 1 until 10 [step 1]) { //[1, 10)*** } for (i in 10 downTo 0 [step 1]) { //[10, 0] }
filter过滤列表:
val li = listOf(...) li.filter(i -> {some function ...}) li.filter {some function with `it`} //it就是li中的项,通过处理it进行过滤
特殊关键字
when
when (var) { val1 -> {expression1} val2 -> {expression2} val3 -> {expression3} else -> {expression4} }
类
class Runoob { //类的属性定义 [private/public] val name: String = …… //不可变属性(未声明变量类型时默认为public) var url: String = …… //可变属性 var city: String = …… fun method1() { } }
构造函数:
class Person(val name: String) { //主构造函数 constructor (name: String, age:Int) : this(name) { //次构造函数 // 初始化... } }
完整的属性定义:
class Person { var lastName: String = "zhang" get() = field.toUpperCase() //field表示当前这个变量 set var heiht: Float = 145.4f private set }
Lambda表达式
val lambda = { var1: Int, var2: Int -> var1 + var2 }
JNI
Java Native Interface(能够在Java代码中通过JNI层调用原生C/C++代码)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人