如何在 Android 中使用字串名稱從資源獲取字串?
簡介
Android 應用程式使用不同型別的字串在不同型別的檢視(如文字檢視、按鈕等)中顯示。在大多數情況下,與其在程式碼中硬編碼字串,不如將這些字串新增到 strings.xml 檔案中,以便於修改和管理整個專案中的這些字串。此外,這將有助於啟用字串的國際化,以便在我們的應用程式中使用。
實現
我們將建立一個簡單的應用程式,其中我們將顯示兩個文字檢視。一個用於我們的標題,另一個用於我們的訊息。在第一個文字檢視中,我們將透過 activity_main.xml 檔案從 strings.xml 檔案顯示文字,在另一個文字檢視中,我們將從 java 檔案將字串設定為我們的文字檢視。
步驟 1:在 Android Studio 中建立新專案
導航到 Android Studio,如下面的螢幕所示。在下面的螢幕中,單擊“新建專案”以建立新的 Android Studio 專案。
單擊“新建專案”後,您將看到下面的螢幕。
在此螢幕中,我們只需選擇“空活動”並單擊“下一步”。單擊“下一步”後,您將看到下面的螢幕。
在此螢幕中,我們只需指定專案名稱。然後包名將自動生成。
注意 - 確保選擇 Java 作為語言。
指定所有詳細資訊後,單擊“完成”以建立新的 Android Studio 專案。
建立專案後,我們將看到 2 個開啟的檔案,即 activity_main.xml 和 MainActivity.java 檔案。
步驟 3:在 string.xml 檔案中新增字串
導航到 app>res>values>strings.xml 檔案,並將以下程式碼新增到其中。程式碼中添加了註釋以詳細瞭解。
<resources> <string name="app_name">Android Application</string> <!-- we will be displaying below string inside our heading text view --> <string name="heading_str">String from resource using its name in Android</string> <!-- we will be displaying below string inside our message text view --> <string name="message_string">Welcome to Tutorials Point</string> </resources>
步驟 4:使用 activity_main.xml
導航到 activity_main.xml。如果此檔案不可見,則要開啟此檔案,在左側窗格中導航到 app>res>layout>activity_main.xml 以開啟此檔案。開啟此檔案後,將以下程式碼新增到其中。程式碼中添加了註釋以詳細瞭解。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="@+id/idRLLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<!-- on below line creating a text view for displaying a heading-->
<TextView
android:id="@+id/idTVHeading"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:gravity="center"
android:padding="4dp"
android:text="@string/heading_str"
android:textAlignment="center"
android:textColor="@color/black"
android:textSize="20sp"
android:textStyle="bold" />
<!-- creating a text view for displaying a message-->
<TextView
android:id="@+id/idTVMessage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/idTVHeading"
android:layout_centerInParent="true"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:gravity="center"
android:padding="4dp"
android:textAlignment="center"
android:textColor="@color/black"
android:textSize="20sp"
android:textStyle="bold" />
</RelativeLayout>
說明 - 在上面的程式碼中,我們建立了一個相對佈局作為根佈局,並在其中建立了一個簡單的文字檢視,在其中顯示應用程式的標題。之後,我們在其中建立了另一個文字檢視,我們將在其中顯示我們的訊息。在第一個文字檢視中,我們直接從 strings.xml 檔案載入訊息到我們的標題文字檢視中。而在我們的第二個文字檢視中,我們將從我們的 java 檔案設定文字。
步驟 5:使用 MainActivity.java 檔案
導航到 MainActivity.java。如果此檔案不可見,則要開啟此檔案,在左側窗格中導航到 app>res>layout>MainActivity.java 以開啟此檔案。開啟此檔案後,將以下程式碼新增到其中。程式碼中添加了註釋以詳細瞭解。
package com.example.androidjavaapp;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.media.Image;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
// on below line we are creating variable for text view.
private TextView msgTV;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// on below line initializing variable with id.
msgTV = findViewById(R.id.idTVMessage);
// on below line we are setting data to our text view from our string.xml file.
msgTV.setText(getResources().getString(R.string.message_string));
}
}
說明 - 在上面的程式碼中,我們首先為文字檢視建立了變數。之後,我們將看到一個 onCreate 方法。在此 onCreate 方法中,我們正在膨脹一個佈局檔案,我們將在其中顯示我們的佈局檔案。在此 onCreate 方法中,我們使用我們在 activity_main.xml 中給出的 id 初始化我們的文字檢視。之後,我們為該文字檢視設定 strings.xml 檔案中的資料。
新增上述程式碼後,我們只需單擊頂部欄中的綠色圖示即可在移動裝置上執行我們的應用程式。
注意 - 確保您已連線到您的真實裝置或模擬器。
輸出
結論
在本文中,我們瞭解瞭如何在 Android Studio 專案中使用 strings.xml 檔案儲存要使用的字串。此外,我們還了解了如何在專案中使用 strings.xml 檔案中的字串。
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP