android 中 TextView的用法[通俗易懂]

2022-11-17 11:06:09 浏览数 (2)

大家好,又见面了,我是你们的朋友全栈君

本文讲解TextView4种常见的用法:

一. 如何显示文本(URL,不同大小、字体、颜色的文本)

activity_main.xml

代码语言:javascript复制
<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

代码语言:javascript复制
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.comn";
        text  = "我的邮箱:123@163.comn";
        text ="我的电话: 13956789918n";
        textView2.setText(text);
        textView2.setMovementMethod(LinkMovementMethod.getInstance());
      
       
        
    }

	
}

二.在TextView 中显示表情图像和文本

图片目录:

activity_main.xml

代码语言:javascript复制
<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

代码语言:javascript复制
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

代码语言:javascript复制
<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

代码语言:javascript复制
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

代码语言:javascript复制
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

代码语言:javascript复制
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

代码语言:javascript复制
<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

代码语言:javascript复制
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());//点击时产生超链接。                    
       
      
       
        
    }

	
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/214271.html原文链接:https://javaforall.cn

0 人点赞