android学习---TextView
1.TextView常用属性
TextView的常用属性有以下一些:
setText(); //设置文本内容,同xml中的android:text setTextSize(); //设置文本字体大小,同xml中的android:textSize setTextColor(); //设置文本颜色,同xml中的android:textColor setBackgroundColor(); //设置背景颜色,同xml中的android:background
此外,还可以在xml中设置一些TextView的属性,如下:
android:autoLink //设置是否显示为可点击的链接。可选值(none/web/email/phone/map/all) android:drawableBottom //在text的下方输出一个drawable(图片) android:drawableLeft //在text的左边输出一个drawable(图片) android:drawableRight //在text的右边输出一个drawable(图片) android:drawableTop //在text的正上方输出一个drawable(图片) android:drawablePadding //设置text与drawable(图片)的间隔,与drawableLeft、drawableRight、drawableTop、drawableBottom一 起使用,可设置为负数,单独使用没有效果 android:ellipsize //设置当文字过长时,该控件该如何显示。可设置如下属性值:"start"省略号显示在开头;
"end”省略号显示在结尾;"middle"省略号显示在中间; "marquee" 以跑马灯的方式显示(动画横向移动) android:gravity //设置文本位置,设置成"center",文本将居中显示 android:linksClickable //设置点击时是否链接,即使设置了autoLink android:marqueeRepeatLimit //在ellipsize设定为marquee时,设置重复滚动的次数,设置为marquee_forever时表示无限次。 android:lines //设置文本的行数,设置两行就显示两行,即使第二行没有数据 android:shadowRadius //设置阴影的半径。设置为0.1就变成字体的颜色了,一般设置为3.0的效果比较好 android:shadowColor //指定文本阴影的颜色,需要与shadowRadius一起使用 android:singleLine //设置单行显示 android:textColorLink //设置文字链接的颜色 android:textScaleX //设置文字之间间隔,默认为1.0f android:textStyle //设置字形 bold(粗体) 0, italic(斜体) 1, bolditalic(又粗又斜) 2, 可以设置一个或多个,用“|”隔开 android:typeface //设置文本字体,必须是以下常量值之一:normal 0, sans 1, serif 2, monospace(等宽字体) 3
2.TextView显示URL文本
在TextView中预定义了一些类似HTML的标签,通过这些标签可以使TextView控件显示不同的颜色、大小和字体的文字。HTML的常用标签有以下一些:
<font>设置文本的字体、字体颜色、字体尺寸,如:<font size ="3" color = "red">This is Some Text!</font>
<big>设置大号字体效果
<small>设置小号字体效果
<i>设置斜体文本效果
<b>设置粗体文本效果
<a>通过使用href属性,创建指向另外一个文档的链接(超链接),如:<a href = http://www.baidu.com>百度</a>
<br>插入一个简单的换行符,注意:在HTML中,<br>标签没有结束标签
<p>自定在其前后创建一些空白
<img>向网页中嵌入一幅图像,该标签有两个必须的属性:src和alt
使用这些标签可以用Html.fromHtml方法将这些标签的字符串转换成CharSequence接口,然后在TextView.setText()中进行设置。如果需要响应设置的HTML标签进行响应,需要设置TextView.setMovementMethod(LinkMovementMethod.getInstance())。
CharSequence为接口类型,大家可能对其有点陌生,但是它的子类肯定会让大家有熟悉的感觉,String、StringBuffer、StringBuilder、SpannableString、SpannableStringBuilder都是其子类,它包括了字符串的所有类,因为面向对象的多态性,在这里把他理解成字符串类的抽象即可。
除了使用HTML标签的方式设定显示文本中的URL地址、邮箱地址、电话等产生超链接出发相应的服务,可以使用android:autoLink属性来设置,以下是android:autoLink属性的介绍:
- None:默认的,不匹配任何连接。
- web:网址。
- email:邮箱。
- phone:电话号码。
- map:匹配映射网址。
- all:匹配所有连接。
实现页面跳转
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="fill_parent" 4 android:layout_height="fill_parent" 5 android:orientation="vertical" > 6 7 <TextView 8 android:id="@+id/textview1" 9 android:layout_width="fill_parent" 10 android:layout_height="wrap_content" 11 android:padding="20sp" /> 12 13 <TextView 14 android:id="@+id/textview2" 15 android:layout_width="fill_parent" 16 android:layout_height="wrap_content" 17 android:autoLink="all" 18 android:padding="20sp" 19 android:textSize="20sp" /> 20 21 </LinearLayout>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.leaf.android; 2 3 import android.R.string; 4 import android.app.Activity; 5 import android.os.Bundle; 6 import android.text.Html; 7 import android.text.method.LinkMovementMethod; 8 import android.widget.TextView; 9 10 public class Main extends Activity { 11 /** Called when the activity is first created. */ 12 private TextView textView1, textView2; 13 14 @Override 15 public void onCreate(Bundle savedInstanceState) { 16 super.onCreate(savedInstanceState); 17 setContentView(R.layout.main); 18 textView1 = (TextView) this.findViewById(R.id.textview1); 19 textView2 = (TextView) this.findViewById(R.id.textview2); 20 21 // 添加一段html的标志 22 String html = "<font color = 'red'><big><i>I Love Android<i><big></font><p>"; 23 html += "<big><a herf = ‘http://www.cnblogs.com/’>博客园</a></big>"; 24 CharSequence charSequence = Html.fromHtml(html); 25 textView1.setText(charSequence); 26 textView1.setMovementMethod(LinkMovementMethod.getInstance());// 点击的时候产生超链接 27 28 String text = "我的博客: http://www.cnblogs.com/lea-fu/\n"; 29 text += "我的邮箱:leaf816@gmail.com\n"; 30 textView2.setText(text); 31 textView2.setMovementMethod(LinkMovementMethod.getInstance());// 点击的时候产生超链接 32 33 } 34 }
效果
textview图文显示
在TextView中显示图片的例子,依然是使用HTML标签的方式定义样式,但是使用的是Html.fromHtml()的另外一个重载的静态方法,可以设定<img>标签中的图像资源。
static Spanned fromHtml(String source,Html.ImageGetter imageGetter,Html.TagHandler tagHandler)
对于这个方法,在imageGetter参数中设定<img>标签中的图像资源文件,tagHandler主要是为了处理Html类无法识别的html标签的情况,一般不会用上,传值为null即可。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.leaf.android; 2 3 import java.lang.reflect.Field; 4 5 import android.R.color; 6 import android.app.Activity; 7 import android.graphics.Color; 8 import android.graphics.drawable.Drawable; 9 import android.os.Bundle; 10 import android.text.Html; 11 import android.text.Html.ImageGetter; 12 import android.text.method.LinkMovementMethod; 13 import android.widget.TextView; 14 15 public class Main extends Activity { 16 /** Called when the activity is first created. */ 17 public int getResourcedId(String name) { 18 try { 19 // 根据资源的ID的变量名获得Field的对象,使用反射机制来实现的 20 Field field = R.drawable.class.getField(name); 21 // 取得并返回资源的id的字段(静态变量)的值,使用反射机制 22 return Integer.parseInt(field.get(null).toString()); 23 } catch (Exception e) { 24 // TODO: handle exception 25 } 26 return 0; 27 } 28 29 @Override 30 public void onCreate(Bundle savedInstanceState) { 31 super.onCreate(savedInstanceState); 32 setContentView(R.layout.main); 33 TextView textView = (TextView) this.findViewById(R.id.textview); 34 textView.setTextColor(Color.BLACK); 35 textView.setBackgroundColor(Color.WHITE); 36 textView.setTextSize(20); 37 String html = "图像1<img src='img1'/>图像2<img src='img2'/>图像3<img src='img3'/><p>"; 38 html += "图像4<a href = 'http://www.baidu.com'><img src='img4'></a>"; 39 CharSequence charSequence = Html.fromHtml(html, new ImageGetter() { 40 41 public Drawable getDrawable(String source) { 42 // TODO Auto-generated method stub 43 // 获得系统资源的信息,比如图片信息 44 Drawable drawable = getResources().getDrawable( 45 getResourcedId(source)); 46 // 第三个图片文件按照50%的比例进行压缩 47 if (source.equals("img3")) { 48 drawable.setBounds(0, 0, drawable.getIntrinsicWidth() / 2, 49 drawable.getIntrinsicHeight() / 2); 50 } else { 51 drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), 52 drawable.getIntrinsicHeight()); 53 } 54 return drawable; 55 } 56 57 }, null); 58 textView.setText(charSequence); 59 textView.setMovementMethod(LinkMovementMethod.getInstance()); 60 } 61 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="fill_parent" 4 android:layout_height="fill_parent" 5 android:orientation="vertical" > 6 7 <TextView 8 android:id="@+id/textview" 9 android:layout_width="fill_parent" 10 android:layout_height="wrap_content" 11 android:layout_margin="10dp" 12 android:background="#FFFFFF" /> 13 14 </LinearLayout>
android实现点击textview弹出activity
在TextView添加点击事件,导航到其他的Activity中。使用SpannableString.setSpan()设定那一段文本需要相应点击事件。与之类似的还有SpannableBuilder对象,他们的关系和String与StringBuilder一样。
void setSpan(Object what,int start,int end,int flags)
在what参数中传递一个抽象类ClickableSpan,需要实现其onClick()方法,此为指定文本的点击相应时间。start和end分别指定需要响应onClick()方法的文本开始与结束。flags设定一个标识,确定使用什么方式选择文本块,一般使用Spanned接口下的SPAN_EXCLUSIVE_EXCLUSIVE对其进行赋值,表示遵循设定的开始于结束位置的文本块。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.leaf.android; 2 3 import android.app.Activity; 4 import android.content.Intent; 5 import android.os.Bundle; 6 import android.text.SpannableString; 7 import android.text.Spanned; 8 import android.text.method.LinkMovementMethod; 9 import android.text.style.ClickableSpan; 10 import android.view.View; 11 import android.widget.TextView; 12 13 public class Main extends Activity { 14 /** Called when the activity is first created. */ 15 @Override 16 public void onCreate(Bundle savedInstanceState) { 17 super.onCreate(savedInstanceState); 18 setContentView(R.layout.main); 19 TextView textView = (TextView) this.findViewById(R.id.textview); 20 TextView textView2 = (TextView) this.findViewById(R.id.textview2); 21 String text1 = "显示Activity1"; 22 String text2 = "显示Activity2"; 23 // 主要用来拆分字符串 24 SpannableString spannableString = new SpannableString(text1); 25 SpannableString spannableString2 = new SpannableString(text2); 26 spannableString.setSpan(new ClickableSpan() { 27 28 @Override 29 public void onClick(View widget) { 30 // TODO Auto-generated method stub 31 Intent intent = new Intent(Main.this, Activity1.class); 32 startActivity(intent); 33 } 34 }, 0, text1.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 35 spannableString2.setSpan(new ClickableSpan() { 36 37 @Override 38 public void onClick(View widget) { 39 // TODO Auto-generated method stub 40 Intent intent = new Intent(Main.this, Activity2.class); 41 startActivity(intent); 42 } 43 }, 0, text2.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 44 textView.setText(spannableString); 45 textView2.setText(spannableString2); 46 textView.setMovementMethod(LinkMovementMethod.getInstance()); 47 textView2.setMovementMethod(LinkMovementMethod.getInstance()); 48 } 49 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.leaf.android; 2 3 import android.os.Bundle; 4 5 public class Activity1 extends Main { 6 7 public Activity1() { 8 // TODO Auto-generated constructor stub 9 10 } 11 @Override 12 public void onCreate(Bundle savedInstanceState) { 13 // TODO Auto-generated method stub 14 super.onCreate(savedInstanceState); 15 setTitle("Activity1"); 16 } 17 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.leaf.android; 2 3 import android.os.Bundle; 4 5 public class Activity2 extends Main { 6 7 public Activity2() { 8 // TODO Auto-generated constructor stub 9 } 10 11 @Override 12 public void onCreate(Bundle savedInstanceState) { 13 // TODO Auto-generated method stub 14 super.onCreate(savedInstanceState); 15 setTitle("Activity2"); 16 } 17 18 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="fill_parent" 4 android:layout_height="fill_parent" 5 android:orientation="vertical" > 6 7 <TextView 8 android:id="@+id/textview" 9 android:layout_width="fill_parent" 10 android:layout_height="wrap_content" 11 android:textSize="20sp" /> 12 13 <TextView 14 android:id="@+id/textview2" 15 android:layout_width="fill_parent" 16 android:layout_height="wrap_content" 17 android:layout_marginTop="20dp" 18 android:textSize="20sp" /> 19 20 </LinearLayout>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <?xml version="1.0" encoding="utf-8"?> 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 package="com.leaf.android" 4 android:versionCode="1" 5 android:versionName="1.0" > 6 7 <uses-sdk android:minSdkVersion="10" /> 8 9 <application 10 android:icon="@drawable/ic_launcher" 11 android:label="@string/app_name" > 12 <activity 13 android:label="@string/app_name" 14 android:name=".Main" > 15 <intent-filter > 16 <action android:name="android.intent.action.MAIN" /> 17 18 <category android:name="android.intent.category.LAUNCHER" /> 19 </intent-filter> 20 </activity> 21 <activity android:name=".Activity1" > 22 </activity> 23 <activity android:name=".Activity2" > 24 </activity> 25 </application> 26 27 </manifest>
效果:
textView中添加跑马灯效果
说到文本显示,最常见的效果就是跑马灯效果,这里以一个例子展示跑马灯的效果,在看代码前,先讲解一下等下会碰到的属性:
- android:elipsize: 如果文本长度大于TextView的显示长度,则隐藏那一部分,可赋值为:none(不隐藏)、start(隐藏开始)、middle(隐藏中间)、end(隐藏结束)、marquee(滚动效果)。
- android:marqueRepeatLimit:设定需要重复动画的次数,传递一个int值,-1为无限循环。
- android:focusable:是否允许获得焦点,传递一个bool值。
- android:focusableInTouchMode:是否在获得焦点时对控件有联系,传递一个bool值。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.leaf.android; 2 3 import android.R.string; 4 import android.app.Activity; 5 import android.os.Bundle; 6 import android.text.Html; 7 import android.text.method.LinkMovementMethod; 8 import android.widget.TextView; 9 10 public class Main extends Activity { 11 /** Called when the activity is first created. */ 12 private TextView textView; 13 14 @Override 15 public void onCreate(Bundle savedInstanceState) { 16 super.onCreate(savedInstanceState); 17 setContentView(R.layout.main); 18 textView = (TextView) this.findViewById(R.id.textview); 19 String html = "框架布局中的子视图总是被绘制到相对于屏幕的左上角上。所有添加到这个布局中的视图都是以层叠的方式显示。第一个添加到<a href = 'http://www.baidu.com'>框架布局</a>中的视图显示在最底层"; 20 CharSequence charSequence = Html.fromHtml(html); 21 textView.setText(charSequence); 22 textView.setMovementMethod(LinkMovementMethod.getInstance()); 23 24 } 25 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="fill_parent" 4 android:layout_height="fill_parent" 5 android:orientation="vertical" > 6 7 <TextView 8 android:id="@+id/textview" 9 android:layout_width="fill_parent" 10 android:layout_height="wrap_content" 11 android:layout_margin="10dp" 12 android:background="#FFFFFF" 13 android:ellipsize="marquee" 14 android:focusable="true" 15 android:focusableInTouchMode="true" 16 android:marqueeRepeatLimit="marquee_forever" 17 android:padding="10dp" 18 android:singleLine="true" 19 android:textColor="#000" 20 android:textSize="20dp" /> 21 22 </LinearLayout>
AutoCompleteTextView
自动完成文本框(AutoCompleteTextView),可以从官方文档上看出,是从EditText继承而来,所以它实际上也是一个文本编辑框,只是多了一个自动提示输入补全的功能。功能类似于:当用户输入一定字符之后,自动完成文本框会显示一个下拉列表,供用户从中选择,当用户选择某个菜单项之后,AutoCompleteTextView会按照的选择自动填写该文本框。
常用属性
因为是继承自EditText,所以AutoCompleteTextView除了可以使提供用Edit的属性和方法之外,还支持如下一些特殊的属性及方法,这里只介绍一些常用的,具体请参见官方文档:
- android:completionHint/setCompletionHint(CharSequence):设置出现下拉列表的提示标题。
- android:completionTjreshold/setThreshold(int):设置至少输入几个字符才会显示提示。
- android:dropDownHeight/setDropHeight(int):设置下拉列表的高度。
- android:dropDownWidth/setDropWidth(int):设置下拉列表的宽度。
- android:popupBackground/setDropDownbackgroundResource(int):设置下拉列表的背景。
填充选择数据
在Android程序中,为了展示数据,通常会用到一个Adapter的接口。没错,这是一个接口,是连接后端数据和前端显示的桥梁,是data souce和UI(View)之间一个重要的纽带。下图展示了Adapter在Android程序中的关系:
对于Adapter,它是一个接口,Android为其声明了各种实现类,对于在AutoCompleteTextView控件中,一般使用ArrayAdapter<T>即可完成功能,对于一些其他实现类的应用场景,以后会慢慢介绍。
ArrayAdapter<T>继承自一个抽象类BaseAdapter,而这个抽象类实现了Adapter接口,所以继承关系应该是:Adapter→BaseAdater→ArrayAdapter<T>。
从名字上可以看出,ArrayAdapter<T>是以一个数组的形式去存储数据的,它也确实是这么做的,并且可以传递一个数组对其进行构造。所以我们只需要填充一个数组对象,就完成ArrayAdapter对象的初始化工作,在把得到的ArrayAdapter对象传递给AutoCompleteTextView控件,即可对其进行选择数据设置。
MultiAutoCompleteTextView
既然讲到了AutoCompleteTextView,那就顺带讲一下MultiAutoCompleteTextView,它继承自AutoCompleteTextView。新扩展的功能是:可以进行多次提示,并且每次指定完成的内容通过符号进行分隔显示。使用MultiAutoCompleteTextView必须实现一个MultiAutoCompleteTextView.Tokenizer接口,用于声明用于选项与选项之间分隔的符号,一般如不特殊指定,可以使用Android为我们提供的实现类MultiAutoCompleteTextView.CommaTokenizer,它设定使用英文逗号","进行分隔选项。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.leaf.android; 2 3 import android.app.Activity; 4 import android.os.Bundle; 5 import android.view.View; 6 import android.widget.ArrayAdapter; 7 import android.widget.AutoCompleteTextView; 8 import android.widget.MultiAutoCompleteTextView; 9 10 public class Main extends Activity { 11 /** Called when the activity is first created. */ 12 private AutoCompleteTextView auto; 13 private MultiAutoCompleteTextView mul; 14 15 @Override 16 public void onCreate(Bundle savedInstanceState) { 17 super.onCreate(savedInstanceState); 18 setContentView(R.layout.main); 19 auto = (AutoCompleteTextView) this.findViewById(R.id.autotext); 20 String[] autoStrings = new String[] { "Google", "Google map", 21 "Google search", "baidu", "baidu map", "baidu music" }; 22 // 定义数组适配器,第二个参数表示适配器的下拉风格 23 ArrayAdapter<String> adapter = new ArrayAdapter<String>(Main.this, 24 android.R.layout.simple_dropdown_item_1line, autoStrings); 25 auto.setAdapter(adapter); 26 //找到自动完成组件 27 28 mul = (MultiAutoCompleteTextView) this.findViewById(R.id.mul); 29 mul.setAdapter(adapter);//为其设置适配器 mul.setTokenizer(new 30 mul.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());//完成对选项的拆分功能,以逗号进行拆分 31 32 33 } 34 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="fill_parent" 4 android:layout_height="fill_parent" 5 android:orientation="vertical" > 6 7 <TextView 8 android:layout_width="fill_parent" 9 android:layout_height="wrap_content" 10 android:text="AutoCompleteTextView" /> 11 12 <AutoCompleteTextView 13 android:id="@+id/autotext" 14 android:layout_width="fill_parent" 15 android:layout_height="wrap_content" > 16 </AutoCompleteTextView> 17 18 <TextView 19 android:layout_width="fill_parent" 20 android:layout_height="wrap_content" 21 android:text="MultiAutoCompleteTextView" /> 22 23 <MultiAutoCompleteTextView 24 android:id="@+id/mul" 25 android:layout_width="fill_parent" 26 android:layout_height="wrap_content" > 27 </MultiAutoCompleteTextView> 28 29 </LinearLayout>