Android 点击按钮,文字改变颜色

package com.yanjun;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {
   /** Called when the activity is first created. */
  Button button;
  TextView textView;
   int[] mcolors;
   int colorNumber = 0;

  @Override
   public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    textView = (TextView) findViewById(R.id.textView1);
    button = (Button) findViewById(R.id.button1);
    mcolors = new int[] { Color.BLACK, Color.BLUE, Color.CYAN,
        Color.DKGRAY, Color.LTGRAY, Color.GRAY, Color.GREEN };
    button.setOnClickListener( new OnClickListener() {

       public void onClick(View v) {
         // TODO Auto-generated method stub
         if (colorNumber < mcolors.length) {
          textView.setTextColor(mcolors[colorNumber]);
          colorNumber++;
        } else {
          colorNumber = 0;
        }
      }
    });
  }
}

本文出自 “Android的一点一滴” 博客,请务必保留此出处http://haiyuanxi.blog.51cto.com/4230602/1010149

你可能感兴趣的:(android,点击按钮,文字改变颜色)