我是靠谱客的博主 大胆小蝴蝶,这篇文章主要介绍Android学习之Textview的使用,现在分享给大家,希望可以做个参考。

TextView显示文本控件

示例一:显示文本(url、不同大小、字体、颜色)

示例二:单击链接弹出Activity

示例三:使用TextView实现跑马灯的效果



示例一:

显示文本(url、不同大小、字体、颜色)

activity_main.xml

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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:orientation="vertical" tools:context="com.example.administrator.textviewdemo.MainActivity"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/textview1" android:padding="20sp"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/textview2" android:autoLink="all" android:padding="20sp"/> </LinearLayout>

MainActivity.java

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public class MainActivity extends AppCompatActivity { private TextView textView1,textView2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView1 = (TextView) findViewById(R.id.textview1); textView2 = (TextView) findViewById(R.id.textview2); //添加一段html的标志 String html = "<font color='red'>I Love android</font><br>"; html+="<font color='#0000ff'><big><i>I Love android</i></big></font><p>"; html+="<big><a href='http://www.baidu.com'>百度</a></big>"; CharSequence charSequence = Html.fromHtml(html); textView1.setText(charSequence); //点击的时候产生超链接 textView1.setMovementMethod(LinkMovementMethod.getInstance()); //在Activity_main_xml中已经设置了android:autoLink="all" String textString = "我的URL:http://www.sina.comn"; textString += "我的电话:9878798789"; textView2.setText(textString); } }

示例二:

单击链接弹出Activity

MainActivity.java

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public class MainActivity extends AppCompatActivity { private TextView textView1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView1 = (TextView) findViewById(R.id.textview1); String str1 = "显示Activity1"; //SpannableString主要用来拆分字符串 SpannableString spannableString1 = new SpannableString(str1); spannableString1.setSpan(new ClickableSpan() { @Override public void onClick(View view) { Intent intent = new Intent(MainActivity.this,Activity1.class); startActivity(intent); } }, 0, str1.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); textView1.setText(spannableString1); textView1.setMovementMethod(LinkMovementMethod.getInstance()); } }

Activity1.java

复制代码
1
2
3
4
5
6
7
8
9
public class Activity1 extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_1); setTitle("这是Activity1"); } }

activity_main.xml

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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:orientation="vertical" tools:context="com.example.administrator.textviewdemo.MainActivity"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/textview1" android:padding="20sp"/> </LinearLayout>

示例三:

使用TextView实现跑马灯的效果

activity_main.xml

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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:orientation="vertical" tools:context="com.example.administrator.textviewdemo.MainActivity"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/textview1" android:singleLine="true" android:ellipsize="marquee" android:marqueeRepeatLimit="marquee_forever" android:focusable="true" android:focusableInTouchMode="true"/> </LinearLayout>

MainActivity.java

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class MainActivity extends AppCompatActivity { private TextView textView1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView1 = (TextView) findViewById(R.id.textview1); //这种格式的话可以在滚动中点击进行链接 String html ="<big><a href='http://www.baidu.com'>百度一下</a></big>"; html+= "【珠海市公安局】遇到这些问题怎么破:一、骗子说网上有你的通缉令,公检法电话找你做笔录。二、物品遗失哪里可以发寻物启示?三、如何辨别谣言?微信关注“珠海公安”,不用你花一分钱的贴身安全秘书。"; html+="<big><a href='http://www.baidu.com'>百度一下</a></big>"; CharSequence charSequence = Html.fromHtml(html); textView1.setText(charSequence); textView1.setMovementMethod(LinkMovementMethod.getInstance()); } }



最后

以上就是大胆小蝴蝶最近收集整理的关于Android学习之Textview的使用的全部内容,更多相关Android学习之Textview内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(52)

评论列表共有 0 条评论

立即
投稿
返回
顶部