Android Studio 学习记录:第三天

一、回顾与总结在前两天的学习中,我掌握了 Android Studio 的基本操作,包括项目结构、布局设计基础(线性布局和相对布局)以及基本组件的使用。我还学会了如何运行和调试应用。这些知识为我今天的进阶学习奠定了坚实的基础。二、学习目标1. 深入学习 ConstraintLayout:掌握 ConstraintLayout 的布局方式和约束规则。2. 学习更多 UI 组件:如 EditText、ImageView 等,并实现更复杂的交互功能。3. 理解事件处理机制:掌握点击事件、长按事件等的处理方法。4. 学习数据存储基础:了解 SharedPreferences 的使用方法。三、ConstraintLayout 布局ConstraintLayout 是一种强大的布局方式,允许开发者通过约束(Constraints)来定义组件的位置和大小。它能够替代传统的嵌套布局,减少布局层级,提高性能。(一)ConstraintLayout 的基本属性• app:layout_constraintTop_toTopOf :组件顶部与目标组件顶部对齐。• app:layout_constraintBottom_toBottomOf :组件底部与目标组件底部对齐。• app:layout_constraintLeft_toLeftOf :组件左侧与目标组件左侧对齐。• app:layout_constraintRight_toRightOf :组件右侧与目标组件右侧对齐。• app:layout_constraintStart_toStartOf :组件起始端与目标组件起始端对齐(支持 RTL 布局)。• app:layout_constraintEnd_toEndOf :组件结束端与目标组件结束端对齐(支持 RTL 布局)。(二)示例代码xml<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">

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello, World!"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent" />

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Click Me"
    app:layout_constraintTop_toBottomOf="@id/textView"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
四、更多 UI 组件的使用(一)EditText EditText 是一个可编辑的文本框,用户可以在其中输入文本。• 常用属性:• android:hint :设置提示文本。• android:inputType :设置输入类型(如 text 、 number 、 password 等)。• 示例代码:xml
(二)ImageView ImageView 用于显示图片。• 常用属性:• android:src :设置图片资源。• android:scaleType :设置图片的缩放方式(如 fitCenter 、 centerCrop 等)。• 示例代码:xml
五、事件处理机制Android 中的事件处理主要通过监听器(Listener)来实现。常见的事件包括点击事件、长按事件等。(一)点击事件可以通过 android:onClick 属性或代码中设置监听器来处理点击事件。• 代码示例:javaButton button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Button Clicked!", Toast.LENGTH_SHORT).show();
}
});
(二)长按事件通过设置 OnLongClickListener 来处理长按事件。• 代码示例:javabutton.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
Toast.makeText(MainActivity.this, "Button Long Pressed!", Toast.LENGTH_SHORT).show();
return true; // 返回 true 表示事件已处理
}
});
六、SharedPreferences 数据存储 SharedPreferences 是一种轻量级的数据存储方式,用于存储简单的键值对数据。(一)保存数据javaSharedPreferences sharedPreferences = getSharedPreferences("MyPrefs", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("name", "Kimi");
editor.putInt("age", 25);
editor.apply(); // 或 editor.commit();
(二)读取数据javaSharedPreferences sharedPreferences = getSharedPreferences("MyPrefs", MODE_PRIVATE);
String name = sharedPreferences.getString("name", "Default Name");
int age = sharedPreferences.getInt("age", 0);
Toast.makeText(MainActivity.this, "Name: " + name + ", Age: " + age, Toast.LENGTH_SHORT).show();
七、今日收获1. 掌握了 ConstraintLayout 的布局方式和约束规则,能够实现更复杂的布局设计。2. 学会了更多 UI 组件(EditText、ImageView 等)的使用方法,并实现了简单的交互功能。3. 理解了事件处理机制,能够处理点击事件和长按事件。4. 学习了 SharedPreferences 的使用方法,能够存储和读取简单的键值对数据。八、明日计划1. 学习更复杂的数据存储方式,如 SQLite 数据库。2. 掌握 Fragment 的使用方法,实现多屏交互。3. 学习如何使用 RecyclerView 来显示列表数据。4. 练习开发一个简单的应用,巩固所学知识。通过今天的深入学习,我对 Android 开发的布局设计、事件处理和数据存储有了更全面的理解。希望在接下来的学习中能够继续提升自己的开发能力,开发出更实用的应用。

posted @ 2025-03-15 21:42  马瑞鑫03  阅读(12)  评论(0)    收藏  举报