简体   繁体   中英

How does Android TextView.SetText(int resid) work?

This an example from 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);
    }
}

The part in question is this:

    int question = mQuestionBank[mCurrentIndex].getQuestion();
    mQuestionTextView.setText(question);

question is an int here, I wonder how this works. More strangely, if I change question into a literal int, eg 1, then the app won't work.

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 . So, if you have a string resource in your strings.xml you can supply it here instead of a literal String .

So, if in your strings.xml you have

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

you could do

myTV.setText(R.string.hello);

and myTV would show "Hello".

When you pass it an int that doesn't match an id in R.strings then you get the exception.

The Strings you pre-define in the xml have dynamically generated ids in the R.java class. For example R.string.question_africa is an integer, pointing to your saved 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.

setText() method can be called on different variables, in your example is setText(int resId) , where resId is a resource id

More: http://developer.android.com/reference/android/widget/TextView.html#setText(int)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM