[英]How does Android TextView.SetText(int resid) work?
這是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);
}
}
有問題的部分是這樣的:
int question = mQuestionBank[mCurrentIndex].getQuestion();
mQuestionTextView.setText(question);
question
是這里的一個int
,我想知道這是如何工作的。 更奇怪的是,如果我將question
更改為文字int,例如1,那么應用程序將無效。
我認為這些文檔描述得更好,所以我不確定他們最近是否更改了它們(或者我上次查看過)。
無論如何,當你提供一個int
作為param
它引用一個resource id
。 因此,如果您在strings.xml
有一個string resource
,則可以在此處提供它而不是文字String
。
所以,如果你的strings.xml
有
<resources>
<string name="hello">Hello!</string>
</resources>
你能做到的
myTV.setText(R.string.hello);
和myTV
會顯示“你好”。
當你傳遞一個與R.strings
中的id
不匹配的int
,你會得到異常。
您在xml中預定義的Strings
在R.java
類中具有動態生成的ID。 例如, R.string.question_africa
是一個整數,指向您保存的String resource
。 你可以打開R.java
類並查看引用你的字符串的確切整數,但它不會對你有任何意義,因為它們是自動生成的。
setText()
方法可以在不同的變量上調用,在你的例子中是setText(int resId)
,其中resId
是一個資源id
更多: http : //developer.android.com/reference/android/widget/TextView.html#setText(int)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.