package cn.com.widget.chenzheng_java; import android.app.Activity; import android.content.res.Resources; import android.graphics.Color; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; public class TextViewActivity extends Activity { private TextView textView; private Button button; private int click_falg = 0; private final String TAG = "通知"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); textView = (TextView) findViewById(R.id.myTextView); button = (Button) findViewById(R.id.myButton); button.setOnClickListener(new MyOnClickListener()); } /** * * @author chenzheng_java * */ private class MyOnClickListener implements View.OnClickListener { @Override public void onClick(View v) { switch (click_falg) { case 0: Log.i(TAG, "第一次点击按钮,改变文本的内容及背景颜色"); /*getString方法可以根据提供的索引ID获取具体的值,该方法定义与context中 * setBackgroundColor可以设置背景颜色 * setText 设置文本内容,记住这里的参数可以有多种形式哦 * */ String firstText = getString(R.string.firstText); textView.setText(firstText); /*这里不知道为何,textView.setBackgroundColor(R.color.blue);无法改变背景色 * 但是通过setBackgroundColor(Color.BLUE)设置却可以。 * */ textView.setBackgroundColor(R.color.blue); click_falg += 1; break; case 1: Log.i(TAG, "第二次点击按钮,改变文本的内容及背景颜色"); CharSequence secondText = TextViewActivity.this .getText(R.string.secondText); textView.setText(secondText); /*getResources可以获取与当前context相关的资源信息Resources * resources.getDrawable(R.color.red);则根据索引获取某个指定的可画的对象 * setBackgroundDrawable则是将这个可画的对象画到背景上 */ Resources resources = TextViewActivity.this.getResources(); Drawable drawable = resources.getDrawable(R.color.red); button.setBackgroundDrawable(drawable); button.setTextColor(Color.BLUE); click_falg += 1; break; default: Log.i(TAG, "第三次点击按钮,我们还原到起点"); click_falg = 0; Toast.makeText(TextViewActivity.this, "重新开始变化了哦", Toast.LENGTH_LONG).show(); button.setBackgroundColor(Color.WHITE); textView.setTextColor(Color.BLUE); break; } } } }