package cn.com.widget.chenzheng_java;</p><p>import android.app.Activity;<br />import android.content.res.Resources;<br />import android.graphics.Color;<br />import android.graphics.drawable.Drawable;<br />import android.os.Bundle;<br />import android.util.Log;<br />import android.view.View;<br />import android.widget.Button;<br />import android.widget.TextView;<br />import android.widget.Toast;</p><p>public class TextViewActivity extends Activity {<br />private TextView textView;<br />private Button button;<br />private int click_falg = 0;<br />private final String TAG = "通知";</p><p>@Override<br />public void onCreate(Bundle savedInstanceState) {<br />super.onCreate(savedInstanceState);<br />setContentView(R.layout.main);</p><p>textView = (TextView) findViewById(R.id.myTextView);<br />button = (Button) findViewById(R.id.myButton);</p><p>button.setOnClickListener(new MyOnClickListener());</p><p>}</p><p>/**<br /> *<br /> * @author chenzheng_java<br /> *<br /> */<br />private class MyOnClickListener implements View.OnClickListener {</p><p>@Override<br />public void onClick(View v) {<br />switch (click_falg) {<br />case 0:<br />Log.i(TAG, "第一次點擊按鈕,改變文本的內容及背景顏色");<br />/*getString方法可以根據提供的索引ID擷取具體的值,該方法定義與context中<br /> * setBackgroundColor可以設定背景顏色<br /> * setText 設定常值內容,記住這裡的參數可以有多種形式哦<br /> * */<br />String firstText = getString(R.string.firstText);<br />textView.setText(firstText);<br />/*這裡不知道為何,textView.setBackgroundColor(R.color.blue);無法改變背景色<br /> * 但是通過setBackgroundColor(Color.BLUE)設定卻可以。<br /> * */<br />textView.setBackgroundColor(R.color.blue);<br />click_falg += 1;<br />break;<br />case 1:<br />Log.i(TAG, "第二次點擊按鈕,改變文本的內容及背景顏色");<br />CharSequence secondText = TextViewActivity.this<br />.getText(R.string.secondText);<br />textView.setText(secondText);<br />/*getResources可以擷取與當前context相關的資源資訊Resources<br /> * resources.getDrawable(R.color.red);則根據索引擷取某個指定的可畫的對象<br /> * setBackgroundDrawable則是將這個可畫的對象畫到背景上<br /> */<br />Resources resources = TextViewActivity.this.getResources();<br />Drawable drawable = resources.getDrawable(R.color.red);<br />button.setBackgroundDrawable(drawable);<br />button.setTextColor(Color.BLUE);<br />click_falg += 1;<br />break;<br />default:<br />Log.i(TAG, "第三次點擊按鈕,我們還原到起點");<br />click_falg = 0;<br />Toast.makeText(TextViewActivity.this, "重新開始變化了哦",<br />Toast.LENGTH_LONG).show();<br />button.setBackgroundColor(Color.WHITE);<br />textView.setTextColor(Color.BLUE);<br />break;<br />}<br />}</p><p>}</p><p>}