创建一个字定义颜色数组,单击按钮时,会根据数组元素的索引值的变化来改变文本的颜色
main.axml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/widget27"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/white"
android:orientation="vertical">
<!--
文字使用mytext作為id使用string.xml中
的textview_str參數 預設文字顏色為按灰色
-->
<TextView
android:id="@+id/mytext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/textview_str"
android:textColor="@drawable/darkgray" />
<!-- 按鈕以mybutton作為id使用string.xml中
的button_str參數
-->
<Button
android:id="@+id/mybutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_str" />
</LinearLayout>
MainActivity.cs
using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
//
using Android.Graphics;
namespace Ex03_13
{
[Activity(Label = "Ex03_13", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
private Button mButton;
private TextView mText;
private Color[] mColors;
private int colornum;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
// Get our button from the layout resource,
// and attach an event to it
//Button button = FindViewById<Button>(Resource.Id.MyButton);
//button.Click += delegate { button.Text = string.Format("{0} clicks!", count++); };
mButton = (Button)FindViewById(Resource.Id.mybutton);
mText = (TextView)FindViewById(Resource.Id.mytext);
mColors = new Color[] { Color.Black, Color.Red, Color.Blue, Color.Green, Color.Magenta, Color.Yellow };
colornum = 0;
mButton.Click += delegate
{
if (colornum < mColors.Length)
{
//mText.setTextColor(mColors[colornum]);
mText.SetTextColor(mColors[colornum]);
colornum++;
}
else
{
colornum = 0;
}
};
}
}
}