雜記之使用代碼動態改變文本、按鈕的顏色

來源:互聯網
上載者:User

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>}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.