Content is user-generated and unverified.

Bài Kiểm Tra Android - Đáp Án

Câu 1 (2 điểm): Vai trò của AndroidManifest.xml

Vai trò của AndroidManifest.xml:

  • Là tệp cấu hình chính của ứng dụng Android, khai báo thông tin cơ bản về app
  • Định nghĩa các thành phần như Activity, Service, Broadcast Receiver
  • Quản lý quyền truy cập và các thiết lập bảo mật
  • Chỉ định phiên bản SDK tối thiểu và mục tiêu

Các khai báo quan trọng:

  • <application>: Thông tin ứng dụng (tên, icon, theme)
  • <activity>: Khai báo các màn hình
  • <uses-permission>: Quyền truy cập hệ thống
  • <uses-sdk>: Phiên bản Android hỗ trợ
  • <intent-filter>: Bộ lọc intent

Ví dụ XML:

xml
<uses-permission android:name="android.permission.INTERNET" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name">
    
    <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>

Câu 2 (2 điểm): So sánh TextView, EditText, Button

Điểm giống nhau:

  • Đều kế thừa từ View class
  • Có thể hiển thị text và áp dụng style
  • Hỗ trợ các thuộc tính chung như layout, padding, margin

Điểm khác nhau:

Thành phầnChức năngTương tácỨng dụng
TextViewHiển thị textChỉ đọcNhãn, thông báo
EditTextNhập liệuCó thể chỉnh sửaForm nhập dữ liệu
ButtonThực hiện hành độngNhấn để kích hoạtNút bấm, điều khiển

Ví dụ trong màn hình đăng nhập:

xml
<TextView
    android:text="Đăng nhập"
    android:textSize="24sp" />

<EditText
    android:id="@+id/etUsername"
    android:hint="Tên đăng nhập"
    android:inputType="text" />

<EditText
    android:id="@+id/etPassword"
    android:hint="Mật khẩu"
    android:inputType="textPassword" />

<Button
    android:id="@+id/btnLogin"
    android:text="Đăng nhập" />

Câu 3 (2 điểm): Các loại Layout trong Android

So sánh các Layout:

LinearLayout:

  • Sắp xếp theo hàng dọc hoặc ngang
  • Đơn giản, dễ sử dụng
  • Hiệu suất tốt với ít thành phần

RelativeLayout:

  • Định vị tương đối giữa các thành phần
  • Linh hoạt hơn LinearLayout
  • Phức tạp hơn, có thể gây hiệu suất kém

ConstraintLayout:

  • Định vị bằng constraints (ràng buộc)
  • Hiệu suất tốt nhất, flat hierarchy
  • Phù hợp cho giao diện phức tạp

Ưu - nhược điểm LinearLayout trong form đăng ký:

Ưu điểm:

  • Dễ triển khai cho form có trường nhập theo thứ tự
  • Code đơn giản, dễ bảo trì
  • Hiệu suất tốt với số lượng trường vừa phải

Nhược điểm:

  • Khó căn chỉnh phức tạp
  • Nested layout có thể gây hiệu suất kém
  • Không linh hoạt với nhiều kích thước màn hình

Câu 4 (2 điểm): Adapter trong ListView và GridView

Khái niệm Adapter:

  • Là cầu nối giữa dữ liệu và View (ListView/GridView)
  • Chuyển đổi dữ liệu thành View hiển thị
  • Quản lý việc tái sử dụng View để tối ưu hiệu suất

Phân biệt ArrayAdapter và BaseAdapter:

ArrayAdapter:

  • Sử dụng cho dữ liệu đơn giản (String, primitive types)
  • Có sẵn layout mặc định
  • Dễ sử dụng, ít tùy biến

BaseAdapter:

  • Cho phép tùy biến hoàn toàn
  • Phù hợp với dữ liệu phức tạp
  • Cần override các method: getCount(), getItem(), getView()

Tái sử dụng View và ViewHolder pattern:

java
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    
    if (convertView == null) {
        convertView = inflater.inflate(R.layout.item_layout, parent, false);
        holder = new ViewHolder();
        holder.textView = convertView.findViewById(R.id.textView);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }
    
    holder.textView.setText(data.get(position));
    return convertView;
}

static class ViewHolder {
    TextView textView;
}

Câu 5 (2 điểm): Ứng dụng ListView với Toast

activity_main.xml:

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"
    android:orientation="vertical">

    <ListView
        android:id="@+id/listViewNames"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

MainActivity.java:

java
package com.example.studentlist;

import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private ListView listViewNames;
    private String[] studentNames = {
        "Nguyễn Văn An",
        "Trần Thị Bình",
        "Lê Minh Cường",
        "Phạm Thị Dung",
        "Hoàng Văn Em",
        "Vũ Thị Phương",
        "Đỗ Minh Giang",
        "Bùi Thị Hoa",
        "Lý Văn Inh",
        "Chu Thị Khánh"
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listViewNames = findViewById(R.id.listViewNames);

        // Tạo ArrayAdapter
        ArrayAdapter<String> adapter = new ArrayAdapter<>(
            this,
            android.R.layout.simple_list_item_1,
            studentNames
        );

        // Gắn adapter vào ListView
        listViewNames.setAdapter(adapter);

        // Xử lý sự kiện nhấn vào item
        listViewNames.setOnItemClickListener((parent, view, position, id) -> {
            String selectedName = studentNames[position];
            Toast.makeText(MainActivity.this, 
                "Xin chào, " + selectedName + "!", 
                Toast.LENGTH_SHORT).show();
        });
    }
}
Content is user-generated and unverified.
    Bài Kiểm Tra Android - Đáp Án | Claude