background image

    本文由 stephenson37 贡献
    pdf 文档可能在 WAP 端浏览体验不佳。建议您优先选择 TXT,或下载源文件到本机查看。

    Android 编程基础
    封面

    1
    Android 编程基础

    Android 

 

基础

UI 

 

编程

1

    更改与显示文字标签

    TextView 标签的使用
      

 

导入

TextView   

import android.widget.TextView;   

 

mainActivity.java 中声明一

 

TextView private TextView mTextView01;   

 

main.xml 

 

中定义一个

TextView <TextView 

android:text="TextView01" android:id="@+id/TextView01" 

android:layout_width="wrap_content" android:layout_height="wrap_content" 
android:layout_x="61px" android:layout_y="69px"> </TextView> 

 

利用

findViewById()方法

 

获取

main.xml 

 

中的

TextView mTextView01 = (TextView) findViewById(R.id. TextView01); 

 

设置

TextView 

 

标签内容

String str_2 = "欢迎来到 Android 的 TextView 

……

世界

"; 

mTextView01.setText(str_2);
    ④

    ⑤
      

 

设置文本超级链接

<TextView android:id="@+id/TextView02" 

android:layout_width="wrap_content" android:layout_height="wrap_content" 
android:autoLink="all" android:text="请访问 Android 开发者: 

http://developer.android.com/index.html" > </TextView>
    2

    Android 

 

编程基础

Color android.graphics.Color 实践 Color 颜色变幻

    android.graphics.Color 包含颜色值

    Color.BLACK Color.BLUE Color.CYAN Color.DKGRAY Color.GRAY Color.GREEN 
Color.LTGRAY Color.MAGENTA Color.RED Color.TRANSPARENT Color.WHITE Color.YELLOW 黑

 

 

 

 

 

 

 

 

 

 

 

色 蓝色 青绿色 灰黑色 灰色 绿色 浅灰色 红紫色 红色 透明 白色 黄色
    编程实现颜色变幻

      

① 新建工程

      

 

修改

mainActivity.java 文件,

 

添加

12   

TextView 对象变量,

 

一个

LinearLayout 对象变量,

 

一个

WC 整数变量,

 

一个

LinearLayout.LayoutParams 变量. package zyf.ManyColorME; /*导入要

使用的包

*/ import android.app.Activity; import android.graphics.Color; import 

android.os.Bundle; import android.widget.LinearLayout; import 
android.widget.TextView; public class ManyColorME extends Activity { /** Called 

when the activity is first created. */ /* 

 

定义使用的对象

*/ private LinearLayout 

myLayout; private LinearLayout.LayoutParams layoutP; private int WC = 

LinearLayout.LayoutParams. WRAP_CONTENT; private TextView black_TV, blue_TV, 
cyan_TV, dkgray_TV, gray_TV, green_TV,ltgray_TV, magenta_TV, red_TV, 

transparent_TV, white_TV, yellow_TV; @Override public void onCreate(Bundle 
savedInstanceState) {

    3
    Android 编程基础

    super super.onCreate(savedInstanceState); /* 实例化一个 LinearLayout

 

布局对象

*/ 

this myLayout = new LinearLayout(this this); /* 设置 LinearLayout

 

的布局为垂直布局

*/ 

myLayout.setOrientation(LinearLayout. VERTICAL); /* 设置 LinearLayout

 

布局背景图片

*/ 

myLayout.setBackgroundResource(R.drawable. back); /* 

 

加载主屏布局

*/ 

setContentView(myLayout); /* 实例化一个 LinearLayout 布局参数,用来添加 View */ layoutP = 
new LinearLayout.LayoutParams(WC, WC); /* 构造实例化 TextView

 

对象

*/ 

constructTextView(); /* 把 TextView 添加到 LinearLayout

 

布局中

*/ addTextView(); /* 设置

TextView

 

文本颜色

*/ setTextViewColor(); /* 设置 TextView

 

文本内容

*/ 

setTextViewText(); } /* 设置 TextView

 

文本内容

*/ public void setTextViewText() 

{ black_TV.setText("黑色"); blue_TV.setText("蓝色"); cyan_TV.setText("青绿色"); 

dkgray_TV.setText("灰黑色"); gray_TV.setText("灰色"); green_TV.setText("绿色"); 
ltgray_TV.setText("浅灰色"); magenta_TV.setText("红紫色"); red_TV.setText("红色"); 

transparent_TV.setText("透明"); white_TV.setText("白色"); yellow_TV.setText("黄色"); 
} /* 设置 TextView

 

文本颜色

*/ public void setTextViewColor()