Android gets the value of string. xml, androidstring. xml

Source: Internet
Author: User

Android gets the value of string. xml, androidstring. xml

Why do we need to store the text in the application separately in the string. xml file?

I. It is for internationalization. to internationalization, you only need to provide another string. xml file, modify all the man information in it to the corresponding language (such as English), and then run the program, the android operating system automatically selects the corresponding string based on the language environment and country of the user's mobile phone. xml file. The mobile phone interface displays English. This is very convenient for internationalization.


2. To reduce the application volume and data redundancy. Assume that "we have been working hard" is used in the application for 1000 times. If you write these words directly at each usage, the program will have 70000 words, these 70000 words account for KB of space. Due to limited mobile phone resources, the CPU processing capability and memory are very limited,KB is a small amount of space for the mobile phone memory. We must remember to "save memory when saving memory" when using mobile phone applications ". If you define these words in string. in xml, this text is referenced by the Resources class in each use, and only occupies 14B. Therefore, it is very effective to reduce the application volume. of course, we may not use so much text during development. However, as mobile application developers, we must develop good programming habits.


There are several differences in getting the values in the string. xml file.

1. in xml files such as AndroidManifest. xml and layout:

Android: text = "@ string/resource_name"

  

2. In activity:

Method 1: this. getString (R. string. resource_name );

Method 2: getResources (). getString (R. string. resource_name );

 

3. In other java files (Context or pplication is required)

Method 1: context. getString (R. string. resource_name );

Method 2: application. getString (R. string. resource_name );



Use of the string. xml file in android

1. Obtain the string and value in string. xml in the program.

<? Xml version = "1.0" encoding = "UTF-8"?>

<Resources>

 <String name = "hello"> Hello World, MainActivity! </String>

 <String name = "app_name"> TestExample01 </string>

</Resources>

Use in Activity:

String appName = (String) this. getResources (). getText (R. string. app_name );

Or:

String appName = (String) this. getResources (). getString (R. string. app_name );


2. Define a string Array (arrays. xml)

<? Xml version = "1.0" encoding = "UTF-8"?>

<Resources>

 <String-array name = "sports">

 <Item> football </item>

 <Item> basketball </item>

 <Item> tai chi </item>

 <Item> ice hockey </item>

 </String-array>

</Resources>

---- GetResources (). getStringArray (R. string. sports );



3. Define the color (colors. xml)

<? Xml version = "1.0" encoding = "UTF-8"?>

<Resources>

 <Color name = "black"> # FFFFFF </color>

</Resources>

--- GetResources (). getDrawable (R. string. black );

--- GetResources (). getColor (R. string. black );



4. Define dimensions (dimens. xml)

<? Xml version = "1.0" encoding = "UTF-8"?>

<Resources>

 <Dimen name = "height"> 80dip </dimen>

</Resources>

--- GetResource (). getDimension (R. string. height );



5. Define a style (styles. xml)

<? Xml version = "1.0" encoding = "UTF-8"?>

<Resources>

 <Style name = "sharpText">

 <Item name = "android: textSize"> 18sp </item>

 <Item name = "android: textColor" >#000000 </item>

 </Style>

</Resources>




Assets folder Resource Access


All files in the assets folder are in the original file format. You need to use AssetManager to read files in the form of byte streams.

1. Call getAssets () in the Activity to obtain the AssetManager reference.

2. Use AssetManager's open (String fileName, int accessMode) method to specify the file to be read and the access mode to obtain the input stream InputStream.

3. Use inputStream of the open file to read the file. After reading the file, remember inputStream. close ().

4. Call AssetManager. close () to close AssetManager.

Note that files from Resources and Assets can only be read but cannot be written.

Read from Raw files as follows:

 Public String getFromRaw (){

 Try {

 InputStreamReader inputReader = new InputStreamReader (getResources (). openRawResource (R. raw. test1 ));

 BufferedReader bufReader = new BufferedReader (inputReader );

 String line = "";

 String Result = "";

 While (line = bufReader. readLine ())! = Null)

 Result + = line;

 Return Result;

 } Catch (Exception e ){

 E. printStackTrace ();

 } 

 }

Read from assets directly

 Public String getFromAssets (String fileName ){

 Try {

 InputStreamReader inputReader = new InputStreamReader (getResources (). getAssets (). open (fileName ));

 BufferedReader bufReader = new BufferedReader (inputReader );

 String line = "";

 String Result = "";

 While (line = bufReader. readLine ())! = Null)

 Result + = line;

 Return Result;

 } Catch (Exception e ){

 E. printStackTrace ();

 }

 }

Of course, if you want to get the memory stream, you can also directly return the memory stream!


How can I reference multiple IDs in android string xml?

Put two textviews together
Control in java code.


Android xml resource array: the content of the xml file is as follows. How can I obtain the value of the attribute name in the item key-value pair?

Read data code: getValue (this, "fourth ");

Public String getValue (Context context, String strText ){
String strData = "";
DocumentBuilderFactory factory = null;
DocumentBuilder builder = null;
Document document = null;
InputStream inputStream = null;
// First find the xml file
Factory = DocumentBuilderFactory. newInstance ();
Try {
// Locate xml and load the document
Builder = factory. newDocumentBuilder ();
InputStream = context. getResources (). getAssets ()
. Open ("data. xml ");
Document = builder. parse (inputStream );
// Find the root Element
Element root = document. getDocumentElement ();
NodeList nodes = root. getElementsByTagName ("item ");
// Traverse all the child nodes of the Root Node
Element cardElement;
String strName;
String strValues;
For (int I = 0; I <nodes. getLength (); I ++ ){
CardElement = (Element) (nodes. item (I ));
StrName = cardElement. getAttribute ("value ");
Element eValues = (Element) cardElement
... The remaining full text>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.