Android的TextView.SetText(int resid)是如何工作的?

4
这是来自Big Nerd Ranch Guide的示例:
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改为字面整数,例如1,那么应用程序就无法正常工作。


你说的“它是如何工作的”是什么意思?strings.xml中的所有字符串都与一个整数值(resid = 资源ID)相关联。当setText()接收到一个整数时,它会查找对应于该整数的字符串资源,并更新TextView - Philip Liberato
2个回答

6
这篇文档可能会更好地解释,因此我不确定它们是否最近已更改(或自上次查看以来)。
无论如何,当您提供一个 int 作为param时,它将引用资源ID。 因此,如果您在strings.xml中拥有字符串资源,则可以在此处提供它,而不是使用字面值的String
例如,在您的strings.xml中有以下内容:
<resources>
    <string name="hello">Hello!</string>
</resources>

您可以做的事情
myTV.setText(R.string.hello);

并且myTV将显示“Hello”。

当您传递的intR.strings中的id不匹配时,会出现异常。


3
在xml中预定义的StringsR.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)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接