gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 12:07:10 26 4
gpt4 key购买 nike

这是来自 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 更改为文字 int,例如1,那么应用程序将无法运行。

最佳答案

The Docs我认为这描述得更好,所以我不确定他们最近是否更改了它们(或者自从我上次查看以来)。

无论如何,当您提供 int 作为 param 时,它指的是 resource id。因此,如果您的 strings.xml 中有一个 string resource,您可以在此处提供它而不是文字 String

因此,如果在您的 strings.xml 中有

<resources>
<string name="hello">Hello!</string>
</resources>

你可以做

myTV.setText(R.string.hello);

myTV 将显示“Hello”。

当你向它传递一个 int 而它与 R.strings 中的 id 不匹配时,你就会得到异常。

关于java - Android TextView.SetText(int resid) 是如何工作的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26557637/

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