Android基本UI控件运用小例子(3)
2016-05-24 22:44
447 查看
点击改变字体的颜色
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 黑色
如图
4、
弹出一个吐死
参数1:context 在哪个activity中去显示
参数2:text要显示的信息
参数3:显示时间的长短 Toast.LENGTH_LONG 长时间 Toast.LENGTH_SHORT 短时间
Toast.makeText(MainActivity.this, colorStr[index], Toast.LENGTH_SHORT).show();
8721
相关文章推荐
- Msql:Incorrect double value: ''for column 'id' at row 1解决
- Android UI设计之RecyclerView
- request获取请求头和请求数据.
- Top K Frequent Elements
- request简介
- ARM64 qemu native build
- iOS开发之高级视图—— UITableView(二)编辑选中行
- android bluetooth蓝牙移植
- AlertDialog.Builder的setCancelable
- Actionscript,AS3,MXML,Flex,Flex Builder,Flash Builder,Flash,AIR,Flash Player之关系
- iOS开发 - 第02篇 - UI进阶 - 06 - PickerView & DatePicker
- 关于UIImage类的对象两种初始化方法的区别
- iOS学习笔记之UITableViewController&UITableView
- Activity.managedQuery()
- quick sort analyse
- hdu 1711 Number Sequence -- (KMP 求数列第一匹配的位置)
- @Intellij IDEA生成serialVersionUID的方法
- Codeforces Round #327 (Div. 2)-Wizards' Duel
- VB语言COM组件开发GUID指定方法
- UIButton(UIEdgeInsets)/设置button上的文字和图片上下垂直居中对齐