[英]How does Android TextView.SetText(int resid) work?
This an example from Big Nerd Ranch Guide: 这是Big Nerd Ranch指南中的一个例子:
package com.example.geoquiz;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class QuizActivity extends ActionBarActivity {
private Button mTrueButton;
private Button mFalseButton;
private Button mNextButton;
private TextView mQuestionTextView;
private TrueFalse[] mQuestionBank = new TrueFalse[] {
new TrueFalse(R.string.question_africa, true),
new TrueFalse(R.string.question_americas, false),
new TrueFalse(R.string.question_asia, false),
new TrueFalse(R.string.question_mideast, true),
new TrueFalse(R.string.question_oceans, true)
};
private int mCurrentIndex = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);
mTrueButton = (Button)findViewById(R.id.true_button);
mFalseButton = (Button)findViewById(R.id.false_button);
mTrueButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(QuizActivity.this, R.string.incorrect_toast, Toast.LENGTH_SHORT).show();
}
});
mFalseButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(QuizActivity.this, R.string.correct_toast, Toast.LENGTH_SHORT).show();
}
});
mQuestionTextView = (TextView)findViewById(R.id.question_text_view);
int question = mQuestionBank[mCurrentIndex].getQuestion();
mQuestionTextView.setText(question);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.quiz, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
The part in question is this: 有问题的部分是这样的:
int question = mQuestionBank[mCurrentIndex].getQuestion();
mQuestionTextView.setText(question);
question
is an int
here, I wonder how this works. question
是这里的一个int
,我想知道这是如何工作的。 More strangely, if I change question
into a literal int, eg 1, then the app won't work. 更奇怪的是,如果我将
question
更改为文字int,例如1,那么应用程序将无效。
The Docs I thought described this better so I'm not sure if they changed them recently (or since I last looked). 我认为这些文档描述得更好,所以我不确定他们最近是否更改了它们(或者我上次查看过)。
Anyway, when you supply an int
as the param
it refers to a resource id
. 无论如何,当你提供一个
int
作为param
它引用一个resource id
。 So, if you have a string resource
in your strings.xml
you can supply it here instead of a literal String
. 因此,如果您在
strings.xml
有一个string resource
,则可以在此处提供它而不是文字String
。
So, if in your strings.xml
you have 所以,如果你的
strings.xml
有
<resources>
<string name="hello">Hello!</string>
</resources>
you could do 你能做到的
myTV.setText(R.string.hello);
and myTV
would show "Hello". 和
myTV
会显示“你好”。
When you pass it an int
that doesn't match an id
in R.strings
then you get the exception. 当你传递一个与
R.strings
中的id
不匹配的int
,你会得到异常。
The Strings
you pre-define in the xml have dynamically generated ids in the R.java
class. 您在xml中预定义的
Strings
在R.java
类中具有动态生成的ID。 For example R.string.question_africa
is an integer, pointing to your saved String resource
. 例如,
R.string.question_africa
是一个整数,指向您保存的String resource
。 You can open the R.java
class and see the exact integers that refer to your strings but it won't mean anything to you since they're automatically generated. 你可以打开
R.java
类并查看引用你的字符串的确切整数,但它不会对你有任何意义,因为它们是自动生成的。
setText()
method can be called on different variables, in your example is setText(int resId)
, where resId
is a resource id setText()
方法可以在不同的变量上调用,在你的例子中是setText(int resId)
,其中resId
是一个资源id
More: http://developer.android.com/reference/android/widget/TextView.html#setText(int) 更多: http : //developer.android.com/reference/android/widget/TextView.html#setText(int)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.