大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。
Jetbrains全家桶1年46,售后保障稳定
本文讲解TextView4种常见的用法:
一. 如何显示文本(URL,不同大小、字体、颜色的文本)
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:id="@+id/textview1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="20sp"
/>
<TextView
android:id="@+id/textview2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="20sp"
android:autoLink="all"
android:textSize="20sp"
/>
</LinearLayout>
MainActivity.java
package com.example.android_textview;
import android.os.Bundle;
import android.app.Activity;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.widget.TextView;
public class MainActivity extends Activity {
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);
//textview1
String html = "<font color='red'>I love android</font><br>";
html+= "<font color='#0000FF'><big><i>I love android</i></big></font><br><p>";
html+="<big><a href='http://www.baidu.com'>百度</a></big>";
CharSequence charSequence = Html.fromHtml(html);
textView1.setText(charSequence);
textView1.setMovementMethod(LinkMovementMethod.getInstance());//点击时产生超链接。
//textview2
String text = "\n我的URL :http://www.sina.com\n";
text += "我的邮箱:123@163.com\n";
text+="我的电话: 13956789918\n";
textView2.setText(text);
textView2.setMovementMethod(LinkMovementMethod.getInstance());
}
}
二.在TextView 中显示表情图像和文本
图片目录:
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:id="@+id/textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="20sp"
/>
</LinearLayout>
MainActivity.java
package com.android.mytextview;
import java.lang.reflect.Field;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.text.Html;
import android.text.Html.ImageGetter;
import android.text.method.LinkMovementMethod;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView)findViewById(R.id.textview);
//textview4
textView.setTextColor(Color.BLACK);
textView.setBackgroundColor(Color.WHITE);
textView.setTextSize(20);
String html2 = "图像1<img src='image1' />图像2<img src='image2' />图像3<img src='image3' /><p>";
html2+="图像4<a href='http://www.baidu.com'><img src='image4' /></a>图像5<img src='image5' />";
CharSequence charSequence2= Html.fromHtml(html2,new ImageGetter() {
public Drawable getDrawable(String source) {
//获得系统资源的信息,比如图片信息
Drawable drawable = getResources().getDrawable(getResourceId(source));
// 第三个图片 按50%压缩
if (source.equals("image3")) {
drawable.setBounds(0, 0, drawable.getIntrinsicWidth()/2, drawable.getIntrinsicHeight()/2);
} else {
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
}
return drawable;
}
}, null);
textView.setText(charSequence2);
textView.setMovementMethod(LinkMovementMethod.getInstance());
}
public int getResourceId(String name){
try {
//根据资源ID的变量名获取Field对象 ,使用java反射寄宿制来实现的。
Field field = R.drawable.class.getField(name);
// 获取并返回资源的 id的字段(静态变量)的值,使用反射机制。
return Integer.parseInt((field.get(null).toString()));
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return 0;
}
}
注意:第三章图片 按照50%等比例压缩,第四章图片 超链接。
三.单击链接弹出Activity
点击“显示Activity1”
点击”显示Activity2“
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:id="@+id/textview1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp" />
<TextView
android:id="@+id/textview2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:layout_marginTop="20dp" />
</LinearLayout>
MainActivity.java
package com.android.mytextview;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends Activity {
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);
String text1 = "显示Activity1";
String text2 = "显示Activity2";
SpannableString s1 = new SpannableString(text1);
SpannableString s2 = new SpannableString(text2);
s1.setSpan(new ClickableSpan() {
@Override
public void onClick(View widget) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this,Activity1.class);
startActivity(intent);
}
}, 0, text1.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
s2.setSpan(new ClickableSpan() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this,Activity2.class);
startActivity(intent);
}
}, 0, text2.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textView1.setText(s1);
textView2.setText(s2);
textView1.setMovementMethod(LinkMovementMethod.getInstance());
textView2.setMovementMethod(LinkMovementMethod.getInstance());
}
}
Activity1.java
package com.android.mytextview;
import android.app.Activity;
import android.os.Bundle;
public class Activity1 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setTitle("Activity1");
}
}
Activity2.java
package com.android.mytextview;
import android.app.Activity;
import android.os.Bundle;
public class Activity2 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle("Activity2");
}
}
四.用TextView实现 跑马灯效果
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:id="@+id/textview1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:focusable="true"
android:focusableInTouchMode="true"
/>
</LinearLayout>
MainActivity.java
package com.example.android_textview;
import android.os.Bundle;
import android.app.Activity;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView textView1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView1 = (TextView)findViewById(R.id.textview1);
//textview1
String html = "4月17日晚,<a href='http://www.weibo.com'>新浪微博</a>终于成功登陆纳斯达克交易所,发行1680万股,最终发行定价17美元,位于此前拟定发行价格区间的下限。按此价格计算,新浪微博将募集资金2.856亿美元,低于预期。";
CharSequence charSequence = Html.fromHtml(html);
textView1.setText(charSequence);
textView1.setMovementMethod(LinkMovementMethod.getInstance());//点击时产生超链接。
}
}
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/214271.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...