gpt4 book ai didi

java - Android 中的 TextView.setText

转载 作者:行者123 更新时间:2023-11-30 08:51:00 26 4
gpt4 key购买 nike

我是 Android 开发的新手,我想了解 TextView.setText 方法的工作原理。在这个例子中,

private TrueFalse[] mQuestionBank = new TrueFalse[]
{
new TrueFalse(R.string.question_oceans, true),
new TrueFalse(R.string.question_mideast, false),
new TrueFalse(R.string.question_africa, false),
};

private int mCurrentIndex = 0;
mQuestionTextView = (TextView) findViewById(R.id.question_text_view);
int question = mQuestionBank[mCurrentIndex].getQuestion();
mQuestionTextView.setText(question);

我不能理解的一点是,setText 只得到一个int 怎么能返回问题文本呢?一个int如何足够?你能给我解释一下吗?谢谢。

最佳答案

这里的int,是string.xml中定义的String值的id。所以 textView.setText(int resid) 获取值作为字符串然后在内部设置。

这是TextView的源代码。

public final void setText(CharSequence text) {
setText(text, mBufferType);
}

// So this get string value from resources and use the above setText().
public final void setText(int resid) {
setText(getContext().getResources().getText(resid));
}

您可以避免从资源中获取字符串值并使您的代码更简单。

关于java - Android 中的 TextView.setText,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30734556/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com