android getText(int resId)和getString(int resId)的使用

今天,简单讲讲android里对于getText(int resId)和getString(int resId)的使用和区别。



最近,我查找资料时发现了getString(int resId)和getText(int resId)这两个函数。在android里,我们通常获取字符资源是通过getString(int resId)来获取的,可是有时候看到代码里也可以通过getText(int resId)来获取String.xml文件的字符资源,那么这两个函数有没有区别呢?在网上查找资料,最终是解决了这个问题。这里记录一下。

        CharSequence chrs = getText(R.string.demo);

        String str = getString(R.string.demo);


        这两种方式有什么不同呢?一定要搞明白,开始实验:


        实验一:strings.xml中的相关代码<string name="demo">demo</string>


        main.xml中用线性布局定义两个文本标签,分别使用两种方式获取demo的值,详细代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/firstText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="" />

    <TextView
        android:id="@+id/secondText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="" />

</LinearLayout>

 注意android:text的值都是空的,下面继续看main.java是怎么处理的:

package com.dy.study.firstbase;

import android.os.Bundle;
import android.widget.TextView;
import android.app.Activity;

public class Main extends Activity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		findViews();

		change();
	}

	private TextView firstText;
	private TextView secondText;

	private void findViews() {
		firstText = (TextView) findViewById(R.id.firstText);
		secondText = (TextView) findViewById(R.id.secondText);
	}

	private void change() {
		CharSequence chs = getText(R.string.demo);
		String str = getString(R.string.demo);
		firstText.setText(chs);
		secondText.setText(str);
	}
}

好,看看实验一的结果:


        可以看到,两个值完全一样,怎么回事?难道这两种方式的效果时完全一样的吗?先不忙下结论,继续实验。

        实验二:把strings.xml中的那句相关代码改成<string name="demo"><b>demo</b></string>,仔细看,中间加了个<b></b>,然后其他都不改变,看结果:


       你看,前面那个标签变了,这说明CharSequence的getText()是获取格式化的常量值,而String的getString()是单单获取值,而舍去了格式化。

       就这么简单吗?可我又想了,如果把它们加起来,会是什么样子呢,继续实验。

       实验三:在实验二的基础上改下main.java的内容:

        main.java

package com.dy.study.firstbase;

import android.os.Bundle;
import android.widget.TextView;
import android.app.Activity;

public class Main extends Activity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		findViews();

		change();
	}

	private TextView firstText;

	private void findViews() {
		firstText = (TextView) findViewById(R.id.firstText);
	}

	private void change() {
		CharSequence chs = getText(R.string.demo);
		String str = getString(R.string.demo);
		firstText.setText(chs + str);
	}
}

这里只让显示标签一的内容,猜想一下是不是一个粗一个细呢?看结果:


       被同化了,这让我想起了java的隐式转换,实验到此为止。

简单讲讲,getText()是获取到String.xml文件的字符资源,包含了字符的格式信息等,而getString()只是获取String.xml文件的字符串,会自动去掉格式的信息。

下面看看android关于getText()和getString()的源码:

1.getString()的源码:

@NonNull
public final String getString(@StringRes int resId) {
    return getResources().getString(resId);
}
@NonNull
public String getString(@StringRes int id) throws NotFoundException {
    return getText(id).toString();
}

有此可见,android的getString()最终调用的还是getText(),不过进行了getText(id).toString()去掉了格式信息,然后转成字符串。

2.getText()的源码:

@NonNull public CharSequence getText(@StringRes int id) throws NotFoundException {
    CharSequence res = mResourcesImpl.getAssets().getResourceText(id);
    if (res != null) {
        return res;
    }
    throw new NotFoundException("String resource ID #0x"
            + Integer.toHexString(id));
}

这个应该是直接从资源文件里根据id获取到保存的字符序列CharSequence 。

所以获取字符资源时,getText()的效果优于getString(),因为getString()内部还是调用getText(id).toString()。


android getText(int resId)和getString(int resId)的使用就讲完了。


就这么简单。

猜你喜欢

转载自blog.csdn.net/bzlj2912009596/article/details/80345518
int