3.13 MonoForAndroid用户人机界面--置换文字颜色的机关

创建一个字定义颜色数组,单击按钮时,会根据数组元素的索引值的变化来改变文本的颜色

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;
                }
            };
        }
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值