Android基本UI控件运用小例子(3)

点击改变字体的颜色

AndroidMainifest中的代码如下

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.changtext1"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.changtext1.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Activity_Main.xml中的代码如下

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/tv_change"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="17dp"
        android:textColor="#ff6600"
        android:text="@string/hello_world1"
        android:textSize="30sp"
         />

</RelativeLayout>

Activity_Main.java中的代码如下

package com.zhuoxin.changetext;

import java.util.Random;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

    TextView tv_ch;
    int[] color = { Color.RED, Color.GREEN, Color.BLUE, Color.YELLOW,
            Color.GRAY };
    int lastIndex;
    String [] colorStr = {"红色","绿色","蓝色","黄色","灰色"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//内部类使用外部的变量,必须把外部类的变量变成常量      
        final Random r = new Random();
        tv_ch=(TextView) findViewById(R.id.tv_change);
        tv_ch.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                int index =0;
                do {
                    index=r.nextInt(color.length);//0
                } while (index==lastIndex);
                lastIndex=index;
//              tag代表标识,msg要调试的信息
//              Log.d("index下标",index+"");//debug  蓝色      error 红色         warn 黄色         info  绿色    v  黑色
                Toast.makeText(MainActivity.this, index+"", Toast.LENGTH_SHORT).show();
                //代码中设置字体颜色的方法
                tv_ch.setTextColor(color[index]);

//              弹出一个吐死
//              参数1:context 在哪个activity中去显示
//              参数2:text要显示的信息
//              参数3:显示时间的长短  Toast.LENGTH_LONG 长时间    Toast.LENGTH_SHORT 短时间
//              Toast.makeText(MainActivity.this, colorStr[index], Toast.LENGTH_SHORT).show();



            }
        }); 
    }

}

总结

1、
Color.RED 是一个int类型的值,通过Color.颜色可以找到相对应的颜色。
2、
内部类使用外部的变量,必须把外部类的变量变成常量
3、
tag代表标识,msg要调试的信息 Log.d(“index下标”,index+”“);//debug 蓝色 error 红色 warn 黄色 info 绿色 v 黑色
如图logcat日志中的显示
4、
弹出一个吐死
参数1:context 在哪个activity中去显示
参数2:text要显示的信息
参数3:显示时间的长短 Toast.LENGTH_LONG 长时间 Toast.LENGTH_SHORT 短时间
Toast.makeText(MainActivity.this, colorStr[index], Toast.LENGTH_SHORT).show();

猜你喜欢

转载自blog.csdn.net/qingxindai/article/details/51494257
今日推荐