Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Android

Cách lấy tên resource từ Resource ID trong Android

Hướng dẫn này minh họa cách lấy tên resource (string) từ ID số nguyên (integer) bằng phương thức getResources().getResourceName(id).

Bước 1: Tạo dự án mới

Mở Android Studio, chọn File → New Project.

Bước 2: Giao diện (res/layout/activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:textSize="16sp"
        android:textStyle="bold" />

</RelativeLayout>

Bước 3: Logic (src/MainActivity.java)

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    TextView textView;

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

        textView = findViewById(R.id.textView);
        int viewId = textView.getId(); // Lấy ID số nguyên (ví dụ: 2131230832)
        String resourceName = getResources().getResourceName(viewId); // "com.example.sample:id/textView"
        textView.setText("Resource ID: " + resourceName);
    }
}

Bước 4: Manifest (androidManifest.xml)

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android" package="app.com.sample">
    <application
        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/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

🔍 Các phương thức hữu ích liên quan

Phương thứcMô tả
getResourceName(int id)Trả về tên đầy đủ: "package:type/name"
getResourceTypeName(int id)Trả về loại: "id", "layout", "drawable", "string"...
getResourceEntryName(int id)Trả về tên entry: "textView", "activity_main"...
getIdentifier(name, type, package)Ngược lại: từ tên → ID

Ví dụ thực tế: Debug layout động

View view = findViewById(someId);
String type = getResources().getResourceTypeName(view.getId());
String name = getResources().getResourceEntryName(view.getId());
Log.d("DEBUG", "View: " + type + "/" + name); // "id/textView"

Chạy ứng dụng

Kết nối thiết bị, nhấn Run. TextView sẽ hiển thị tên resource đầy đủ của chính nó.