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;
        }
      }
    });
  }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章