赞助广告

 

年份

最新评论

评论 RSS

TextView实现跑马灯效果

clock 一月 6, 2013 14:02 by author alex
TextView实现跑马灯效果非常有用,可以用于消息提醒,广告等。 main.xml: Main.java:

TextView单击超链接跳转到另一个Activity

clock 一月 6, 2013 13:50 by author alex
显示新闻等HTML内容时,有时候需要跳转到另一个Activity完成一些特殊功能。通过TextView如何实现呢? main.xml: Main.java: onCreate事件中还需设置textView绑定:

TextView显示表情图像和文字

clock 一月 4, 2013 14:58 by author alex
资源文件添加表情图片 在Main.java中定义方法 在onCreate方法中 显示结果:

Android开发过程中TextView格式化显示HTML

clock 一月 4, 2013 14:01 by author alex
在android中,TextView通常用来显示文本文字,那么如何显示HTML,电话号码,email等特殊格式文字呢! 需要用到TextView的setMovementMethod(LinkMovementMethod) 实例如下:

通过Button改变TextView文字颜色

clock 四月 2, 2011 10:12 by author alex
res/layout/main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/tvText" android:text="@string/tv_name"/> <Button android:layout_width="wrap_content" android:id="@+id/btnChangeColor" android:layout_height="wrap_content" android:text="@string/btn_name"></Button> </LinearLayout> src/ex03_12.java package gphone.ex03_12; import android.app.Activity; import android.graphics.Color; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; public class EX03_12 extends Activity { Button btnChangeColor=null; TextView tvText=null; //用于存储颜色 int[] colors=null; int color_index; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //存储颜色 colors=new int[]{ Color.RED, Color.BLUE, Color.YELLOW, Color.GREEN, Color.GRAY }; color_index=0; tvText=(TextView)EX03_12.this.findViewById(R.id.tvText); btnChangeColor=(Button)EX03_12.this.findViewById(R.id.btnChangeColor); btnChangeColor.setOnClickListener(new Button.OnClickListener(){ @Override public void onClick(View v) { // 按顺序显示定义的颜色 if(color_index<colors.length) { tvText.setTextColor(colors[color_index]); } else { color_index=0; color_index++; } } }); } } 运行结果

TextView超链接输出

clock 三月 11, 2011 09:46 by author alex
android中的TextView类似于ASP.NET的中的Label,HTML中的<label></lable>。 如果在代码中这么做: package idroidgame.ActivityTest; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; public class ActivityTest extends Activity { TextView tv=null; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); tv=(TextView)this.findViewById(R.id.tvHelloWorld); String str="<a href='http://www.xnadevelop.com'></a>"; tv.setText(str); } } 你会发现在手机中运行<a href='http://www.xnadevelop.com'></a> 会原模原样输出。 需要在程序中显示超链接可以这么做。 1.修改TextView的标记增加android:autoLink=”all” <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/tvHelloWorld" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" android:autoLink="all" /> </LinearLayout> 不需要写a标记,应用程序会根据http://判断是否将该文本设置为超链接 tv=(TextView)this.findViewById(R.id.tvHelloWorld); String str="http://www.xnadevelop.com"; tv.setText(str); 2.不需要在布局文件中修改TextView,在程序中实现: package idroidgame.ActivityTest; import android.app.Activity; import android.os.Bundle; import android.text.util.Linkify; import android.widget.TextView; public class ActivityTest extends Activity { TextView tv=null; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); tv=(TextView)this.findViewById(R.id.tvHelloWorld); tv.setAutoLinkMask(Linkify.ALL); String str="http://www.xnadevelop.com"; tv.setMovementMethod(LinkMovementMethod.getInstance()); tv.setText(str); } } Linkify有很多选项 ,对应在布局文件中 的选项 3.格式化输出文本 package idroidgame.ActivityTest; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; import android.text.Html; public class ActivityTest extends Activity { TextView tv=null; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); tv=(TextView)this.findViewById(R.id.tvHelloWorld); String str="<a href='http://www.xnadevelop.com'>xnadevelop.com</a>"; tv.setMovementMethod(LinkMovementMethod.getInstance()); tv.setText(Html.fromHtml(str)); } } 这时候与前两个方法不同,要输出超链接不许指定A标记。 4.用Spannable package idroidgame.ActivityTest; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; import android.text.SpannableString; import android.text.Spanned; import android.text.style.URLSpan; public class ActivityTest extends Activity { TextView tv=null; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); tv=(TextView)this.findViewById(R.id.tvHelloWorld); SpannableString ss=new SpannableString("http://xnadevelop.com"); ss.setSpan(new URLSpan("http://www.xnadevelop.com"),0, 21,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); tv.setMovementMethod(LinkMovementMethod.getInstance()); tv.setText(ss); } } 该方法只能显示超链接样式,不能点。   注意:2,3,4方法必须加入tv.setMovementMethod(LinkMovementMethod.getInstance());

更改显示TextView显示

clock 三月 10, 2011 11:28 by author alex
首先打开”main.xml”布局文件 ,添加TextView命名为tvHelloWorld:保存。 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/tvHelloWorld" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> </LinearLayout> 这时候Eclipse编译器会自动生成TextView代码,打开R.java文件 可以看到 /* AUTO-GENERATED FILE. DO NOT MODIFY. * * This class was automatically generated by the * aapt tool from the resource data it found. It * should not be modified by hand. */ package idroidgame.ActivityTest; public final class R { public static final class attr { } public static final class drawable { public static final int icon=0x7f020000; } public static final class id { public static final int tvHelloWorld=0x7f050000; } public static final class layout { public static final int main=0x7f030000; } public static final class string { public static final int app_name=0x7f040001; public static final int hello=0x7f040000; } } 然后在ActivityText.java中输入 package idroidgame.ActivityTest; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; public class ActivityTest extends Activity { TextView tv=null; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); tv=(TextView)this.findViewById(R.id.tvHelloWorld); String str="设置TextView的值"; tv.setText(str); } } TextView的setText有很多重载方法;

友情链接赞助