安卓開發之添加資源

Step1: 添加相應的資源文件(例如strings.xml)

<string name="true_button">True</string>
<string name="false_button">False</string>

Step2:修改相應的Layout文件(注意佈局文件中對資源文件的引用[@string/true_button])

<Button
    android:id="@+id/true_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/true_button" />

Tips: Notice that there is a + sign in the values for android:id but not in the values for android:text. This
is because you are creating the IDs and only referencing the strings. (注意這裏的+號,代表你需要創建相應的實體對象。)

Step3: 在Controller中添加相應的實體對象以及邏輯(QuizActivity)

private Button mTrueButton;
private Button mFalseButton;
mTrueButton = (Button) findViewById(R.id.true_button);
mTrueButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        checkAnswer(true);
    }
});

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章