posts - 12,  comments - 21,  trackbacks - 0
  1 package com.ex05;
 2 
 3 import android.app.Activity;
 4 import android.graphics.Color; //必须引用graphics.Color  才能使用Color对象
 5 import android.os.Bundle;
 6 import android.view.View;
 7 import android.view.View.OnClickListener;
 8 import android.widget.Button;
 9 import android.widget.TextView;
10 
11 public class ex05 extends Activity {
12     private Button b1;
13     private TextView tv1;
14     private int mColors[];
15     private int colornum;
16     /** Called when the activity is first created. */
17     @Override
18     public void onCreate(Bundle savedInstanceState) {
19         super.onCreate(savedInstanceState);
20         setContentView(R.layout.main);
21         //定义一个TextView 来显示文字字体颜色的变化
22         tv1=(TextView)findViewById(R.id.textview);
23         //定义一个Button,当单击时,文字一次改变颜色
24         b1=(Button)findViewById(R.id.button);
25         //定义颜色数组,文字颜色按数组颜色的顺序变化
26         mColors=new int[]{ Color.BLACK,Color.BLUE,Color.RED,Color.GREEN,Color.MAGENTA,Color.YELLOW};
27         colornum=0;
28         //单击按钮的处理
29         b1.setOnClickListener(new Button.OnClickListener(){
30             @Override
31             public void onClick(View v){
32             if (colornum<mColors.length)
33             {
34                 tv1.setTextColor(mColors[colornum]);
35                 colornum++;
36             }
37             else
38                 colornum=0;
39             }       
40         });
41     }
42 }
 布局文件main.xml:
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:orientation="vertical"
 4     android:layout_width="fill_parent"
 5     android:layout_height="fill_parent"
 6     >
 7 <TextView  
 8     android:id="@+id/textview"
 9     android:layout_width="fill_parent" 
10     android:layout_height="wrap_content" 
11     android:text="@string/hello"
12     />
13 <Button android:text="按我" 
14 android:id="@+id/button"
15  android:layout_width="wrap_content"
16   android:layout_height="wrap_content"
17   ></Button>
18 </LinearLayout>
19 
posted on 2011-05-28 22:21 DoubleW 阅读(1363) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2011年6月>
2930311234
567891011
12131415161718
19202122232425
262728293012
3456789

常用链接

留言簿(3)

随笔档案(12)

文章档案(2)

最新随笔

搜索

  •  

积分与排名

  • 积分 - 28506
  • 排名 - 662

最新随笔

最新评论

阅读排行榜

评论排行榜