TextView

article/2025/10/28 5:22:53

文本控件

显示富文本(URL、不同大小、字体、颜色的文本)

在TextView中预定义了一些类似HTML标签(不区分大小写),通过这些标签,我们可以使TextView控件显示不同的颜色、大小、字体的文字。
常见的标签如下:

名称描述
<font>设置颜色和字体 ,只支持color和face两个属性
<big>大号字体
<small>小号字体
<i>斜体
<b>粗体
<tt>等宽字体(Monospace)
<br>换行(行与行之间没有空行),相当于\n
<p>换行(行与行之间有空行),相当于\n\n. 对于带有标签的文本,直接使\n无法换行,只能使用<br> 或者<p>
<a>超链接
<img>插入图像,只有一个src属性

虽然和HTML标签类似,但是并不具备HTML标签的全部功能。

不能将带有标签的字符串直接使用TextView.setText()的方法进行设置,需要使用Html.fromHtml()将带有标签的字符串转换成CharSequence对象,然后再使用TextView.setText()方法进行设置。

如果想要在显示的文本中将URL、Email、电话号码等特殊内容高亮显示,并在单击的时候触发相关的动作(URL会调用浏览器显示网址,电话号码会在拨号界面显示电话号),可以通过设置<TextView>标签的android.autoLink属性来实现

名称描述
none不匹配任何链接(默认值)
web匹配Web网址
email匹配Email
phone匹配电话号码
map匹配映射地址
all匹配所有的连接

Demo:
android:autoLink=”all”

 <!--用于显示不同颜色、字体、大小的文字--><TextView
        android:id="@+id/id_tv_richText1"android:layout_width="wrap_content"android:layout_height="wrap_content" /><!--用于显示带URL地址、Email地址、电话号码的文字--><TextView
        android:id="@+id/id_tv_richText2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:autoLink="all"/>

public class RichTextViewAct extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_rich_text_view);init();}private void init() {TextView textView1 = (TextView) findViewById(R.id.id_tv_richText1);TextView textView2 = (TextView) findViewById(R.id.id_tv_richText2);String html = "<font color='red'>I Love Android</font><br>";html += "<font color='#0000FF'><big><i>I Love Android</i></big></font><p>";html += "<font color='@" + android.R.color.holo_green_dark + "'><tt><b><big><u>I Love Android</u></big></b></tt></font><p>";html += "<big><a href='http://www.baidu.com'>百度</a></big>";Log.e("html内容:", html);// 将预定义标签的字符串转换成CharSequence对象CharSequence charSequence = Html.fromHtml(html);Log.e("charSequence:", charSequence.toString());// 为第一个TextView设置文本textView1.setText(charSequence);// 这行代码只管重要,没有,无法单击链接调用浏览器显示网页textView1.setMovementMethod(LinkMovementMethod.getInstance());// 第二个TextVie要显示的文本StringBuffer sb = new StringBuffer();sb.append("我的URL: http://www.baidu.com\n");sb.append("我的Email: tttt@gmail.com\n");sb.append("我的电话:12345678909");textView2.setText(sb.toString());textView2.setMovementMethod(LinkMovementMethod.getInstance());}
}

这里写图片描述

注意
在调用setText方法设置文本完成后,还需要调用 setMovementMethod方法设置一个MovementMethod对象。 由于本例中<a>标签是链接,因此,需要使用LinkMovementMethod.getInstance()方法获得MovementMethod对象,该对象可以使单击浏览器时显示指定的网页,如果不设置MovementMethod对象,虽然可以正常显示a标签指定的链接,但是单击链接任何反应。


在TextView中显示 表情图像和文字

<img>标签可以实现。

img标签只有一个src属性,该属性原则上应该指向一个图像地址或可以找到某个图像资源的唯一标识,但是系统并不会直接根据src属性所指的值自动获取和显示图像,需要开发人员解析。 解析src属性值的工作需要在ImageGetter对象的getDrawable方法中完成。

ImageGetter是个接口。使用过Html.fromHtml方法的如下重载形式会比较熟悉它。

public static Spanned fromHtml(String source,ImageGetter imageGetter ,TagHandler tagHandler);

fromHtml方法有如下三个参数:

  • source:包含Html标签的字符串
  • imageGetter:ImageGetter对象。当系统解析到img标签时就是调用ImageGetter对象的getDrawable方法,并将src属性传入getDrawable方法中。至于src属性值的具体含义,就要在getDrawable方法中确定了。 getDrawable方法返回的是一个Drawable对象。我们可以从res/drawable资源、SD卡或者网络获得资源,并封装成Drawable对象。
  • tagHandler:TagHandler对象,这个参数使用的并不多。当系统处理每一个标签的时候都会调用该对象的handleTag方法,如果不是用该参数,可以设置为null.

Demo: 5张图片,存放在res/drawable文件夹下,在一个TextView中以不同的大小显示这5张图片,并在其中插入相应的文字。

由于无法直接使用文件名来引用res/drawable中的图像资源,我们使用反射技术从R.drawable类中通过图像资源名称获取对应的图像资源ID,实现的原理就是R.drawable类中的相应的资源ID变量名就是图像文件的文件名。

public class MultTextPicAct extends Activity {private TextView tv_textAndPic;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_NO_TITLE);setContentView(R.layout.activity_mult_text_pic);initView();}/*** 初始化视图*/private void initView() {// 组件初始化tv_textAndPic = (TextView) findViewById(R.id.id_tv_textAndPic);tv_textAndPic.setTextSize(20f);tv_textAndPic.setBackgroundColor(Color.WHITE);// 定义图文混编String html = "图片1<img src='flag_mark_blue'/>图片2<img src='flag_mark_gray'/><p>";html += "图片3<img src='tag_blue'/><br>";html += "图片4<a href='http://www.baidu.com'><img src='tag_orange'/></a><p>";html += "图片5<img src='tag_red'/>";//使用Html.fromHtml()转换包含Html标签的文本,需要指定第二个参数CharSequence charSequence = Html.fromHtml(html, new Html.ImageGetter() {@Overridepublic Drawable getDrawable(String source) {// 装载图像资源Drawable drawable = getResources().getDrawable(getResourceId(source));// 第三个图片按50%等比压缩 ,其余按原大小显示if ("tag_blue".equals(source)) {drawable.setBounds(0, 0, drawable.getIntrinsicWidth() / 2, drawable.getIntrinsicHeight() / 2);} else {drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());}return drawable;}}, null);tv_textAndPic.setText(charSequence);// 有<a>超链接标签,需设置LinkMovementMethod,否则点击无相应tv_textAndPic.setMovementMethod(LinkMovementMethod.getInstance());}/*** 利用反射从R.drawable类中通过图像资源名称获取对应的图像资源ID** @param name 表示res/drawable中的图像文件名(不含扩展名)* @return 图像资源ID*/private int getResourceId(String name) {try {// 根据资源的ID变量名(也就是图像资源的文件名),获得Field字段Field field = R.drawable.class.getField(name);// 取得并返回资源ID(静态变量)的值return Integer.valueOf(field.get(null).toString());} catch (NoSuchFieldException e) {e.printStackTrace();} catch (IllegalAccessException e) {e.printStackTrace();}return 0;}
}

注意:在getDrawable方法中获取到图像资源的drawable对象后,必须使用Drawable.setBounds方法设置图像的显示区域,否则显示区域的面积为0,也就不会在TextView中显示图像了。其中第三个图像等比缩小了50%,显示效果如下

这里写图片描述


单击链接弹出Activity

我们知道通过<a>标签以及TextView自动识别的特殊文本(网址 电话 Email等),这些都可以通过单击操作来触发不同的动作。虽然这些单击动作已经可以满足大部分的需求了,但是如果要想在单击链接的时候执行任意的自定义的动作,就需要学习下面的内容了。

在Android中,Span表示一段文本的效果,例如链接形式,图像,带颜色的文本等。
所有的Span类都在android.text.style包中。

Demo:

准备一个TextView,点击跳转到Activity。

我们使用SpannableString对象来设置Span。

SpannableString和SpannableBuilder的区别:SpannableString不允许修改文本,只允许设置Span,而SpannableBulilder既允许修改文本,也允许设置Span。

public class Jump2Activity extends Activity {private TextView tv_jump ;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_NO_TITLE);setContentView(R.layout.activity_jump2);initData();}/*** 点击TextView弹出Activity*/private void initData() {tv_jump = (TextView)findViewById(R.id.id_tv_jump2Act);String text = "显示Activity";// 将文本转换成SpannableString对象SpannableString spannableString = new SpannableString(text);// 将text中的所有文本设置成ClickableSpan对象,并实现onClick方法spannableString.setSpan(new ClickableSpan() {// 在onClick方法中可以编写单击链接时要执行的动作@Overridepublic void onClick(View widget) {startActivity(new Intent(Jump2Activity.this,JumpTerminalAct.class));}},0,text.length() , Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);// 设置控件显示内容tv_jump.setText(spannableString);// 在点击链接时凡是要有执行的动作,都必须要设置MovementMethod对象tv_jump.setMovementMethod(LinkMovementMethod.getInstance());}
}

方法说明setSpan 4个参数

 public void setSpan(Object what, int start, int end, int flags) {super.setSpan(what, start, end, flags);}

第一个参数需要设置一个ClickableSpan对象
第二个和第三个参数表示要设置成Span的某段文本的起始位置和终止位置。
第四个参数是一个标志,在本例中设置成了Spanned.SPAN_EXCLUSIVE_EXCLUSIVE,该标志在TextView中的意义不大,单在EditText控件中表示的含义:在当前Span效果的前后输入字符串时并不应用Span的效果。

还有其他的几个值:

Spanned.SPAN_EXCLUSIVE_INCLUSIVE :在Span前面输入的字符不应用Span的效果,后面输入的字符应用Span的效果

Spanned.SPAN_INCLUSIVE_INCLUSIVE:在Span前面输入的字符应用Span的效果,后面输入的字符不应用Span的效果

Spanned.SPAN_INCLUSIVE_EXCLUSIVE:在Span前后输入的字符都应用Span的效果。


为指定文字添加背景

从上面的例子中我们可以总结出 设置字符串中的某个子字符串的样式(变成可单击的链接、设置字体等)步骤如下:

  1. 将字符串转换成SpannableString或者SpannableBuilder对象
  2. 获得要设置样式的子字符串在原字符串中的位置和子字符串后面的字符的位置,即start和end
  3. 创建一个Span对象(所有android.text.style包中的XXXSpan类创建的对象的统称,XXX标识URL、BackGround等类的前缀)
  4. 使用setSpan方法设置一个span对象,即将要设置样子的子字符串转换为Span对象
  5. 用处理完的SpannableString或者SpannableBuilder对象设置相应的控件(例如TextView、EditText、Button等)

在SDK的android.text.style有很多现成的Span对象,例如BackgroundColorSpan,该类的功能是设置指定字符串的背景色。
例如:

TextView textView = (TextView)findViewById(R.id.textView);
String text = "<没有背景><黄色背景>";
// 第一步:将字符串转换成为SpannableString对象
SpannableString s = new SpannableString(text);
// 第二步:确定要设置的子字符串的star和end
int start = 6 ;
int end = 12 ;
// 第三步: 创建BackgroundColorSpan对象
BackgroundColorSpan b = new BackgroundColor(Color.YELLOW);
// 第四步:使用setSpan将指定的子字符串转换成BackgroundColorSpan对象
s.setSpan(b,start,end,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
// 第五步:用SpannableString对象设置TextView控件
textView.setText(s);

BackgroundColorSpan只能够设置文字的背景色,为了更加的通用,自定义一个ColorSpan类,使其能够同时设置文字颜色和背景色(android.text.style.ForegroundColorSpan可以设置文字颜色,但并没有可以同事设置背景和文字颜色的Span类)。

如果需要处理链接动作,必须要继承ClickableSpan类,本例我们只是设置文字和背景颜色,并不需要处理任何动作,因此只需要从CharacterStyle类继承即可。事实上,ClickableSpan也是CharacterStyle的子类。

代码如下:


import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.style.BackgroundColorSpan;
import android.text.style.CharacterStyle;
import android.widget.TextView;import com.turing.base.R;public class AddBackgroundAct extends Activity {private TextView tv_addbBackground;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_add_background);initView();}private void initView() {tv_addbBackground = (TextView) findViewById(R.id.id_tv_addBackground);String text = "<没有背景><黄色背景>\n\n<蓝色背景,红色文字>";SpannableString spannableString = new SpannableString(text);int start = 6;int end = 12;BackgroundColorSpan backgroundColorSpan = new BackgroundColorSpan(Color.YELLOW);spannableString.setSpan(backgroundColorSpan, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);// <蓝色背景,红色文字> (每个 “\n”算一个长度)start = 14;end = text.length();// 创建ColorSpanColorSpan colorSpan = new ColorSpan(Color.RED, Color.BLUE);// 将文字转换为ColorSpan对象spannableString.setSpan(colorSpan, start,end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);tv_addbBackground.setText(spannableString);TextView tv = (TextView)findViewById(R.id.id_tv_tv2);tv.setText("一整个字符串的普通的背景设置,文字颜色设置");tv.setBackgroundColor(Color.GREEN);tv.setTextColor(Color.DKGRAY);}/*** 自定义Span类,可以同时设置文字颜色和背景色*/class ColorSpan extends CharacterStyle {private int mTextColor;private int mBackgroundColor;public ColorSpan(int mTextColor, int mBackgroundColor) {this.mTextColor = mTextColor;this.mBackgroundColor = mBackgroundColor;}// 重写updateDrawState方法@Overridepublic void updateDrawState(TextPaint tp) {tp.bgColor = mBackgroundColor;tp.setColor(mTextColor);}}
}

这里写图片描述


总结:

  1. 将android:autoLink的属性值设置为true,系统会自动识别E-mail、电话、网址等特殊文本
  2. 使用Html标签,例如 <font> <img>等,不要设置android:autoLink属性
  3. 在java代码中直接使用Span对象来设置文本样式。这种方法需要将文本转换成为一个SpannableString或者SpannableBuilder对象,然后在SpannableString或者SpannableBuilder对象中使用setSpan方法将需要设置样式的文本转换成相应的Span对象
  4. 在字符串资源中,使用<a>标签(仅支持a标签)设置可点击的链接,不要设置android:autoLink属性。例如
<string name="link_text"><a href="tel:123456">打电话</a>
</string>

然后在TextView标签中引用

.....
android:text="@string/link_text"

带边框的TextView

两种方式:

  1. 编写一个继承TextView类的自定义控件,重写onDraw()绘制边框
  2. 使用9-patch(*.9.png)格式的图像作为TextView的背景图来设置边框(这个背景图需要有一个边框)

通过第一中方式实现:

public class BorderTextView extends TextView {public BorderTextView(Context context, AttributeSet attrs) {super(context, attrs);}@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);Paint paint = new Paint();// 设置所绘制的边框颜色为黑色paint.setColor(Color.BLACK);// 绘制上边框canvas.drawLine(0, 0, this.getWidth() - 1, 0, paint);// 绘制左边框canvas.drawLine(0, 0, 0, this.getHeight() - 1, paint);// 绘制右边框canvas.drawLine(this.getWidth() - 1, 0, this.getWidth() - 1, this.getHeight() - 1, paint);// 绘制下边框canvas.drawLine(0, this.getHeight() - 1, this.getWidth() - 1, this.getHeight() - 1, paint);}
}

XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"><com.turing.base.activity.textViewAct.BorderTextView
        android:id="@+id/id_tv_borderTV"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_margin="10dp"android:padding="10dp"android:gravity="center"android:text="自定义TextView绘制边框"/></RelativeLayout>

这里写图片描述


设置行间距

如果TextView控件中显示了多行文本,会有一个默认的行间距。
如果要改默认的行间距,三种方法:

  1. 布局文件中使用android:lineSpacingExtra 或者 android:lineSpacingMultiplier属性设置行间距。

android:lineSpacingExtra设置精确的行间距,例如
android:lineSpacingExtra="20dp"

android:lineSpacingMultiplier 属性设置的是默认行间距的倍数。

如果同时设置了这两个属性,以较大行间距为准。

  1. 使用Style资源设置行间距
  2. 使用setLineSpacing方法设置行间距。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"android:background="#000"><!--使用android:lineSpacingExtra属性设置行间距--><TextView
        android:id="@+id/id_tv_lineSpace"android:layout_width="match_parent"android:layout_height="wrap_content"android:padding="10dp"android:layout_margin="10dp"android:background="#FFF"android:lineSpacingExtra="20dp"android:textColor="#000"android:text="第一行的文本\n第二行的文本(20dp)"/><!--使用android:lineSpacingMultipile属性设置行间距--><TextView
        android:id="@+id/id_tv_lineSpace2"android:layout_width="match_parent"android:layout_height="wrap_content"android:padding="10dp"android:layout_margin="10dp"android:background="#FFF"android:lineSpacingMultiplier="1.8"android:textColor="#000"android:text="第一行的文本\n第二行的文本(行间距是默认行间距的1.8倍)"/><!--使用style定义行间距--><TextView
        android:id="@+id/id_tv_lineSpace3"android:layout_width="match_parent"android:layout_height="wrap_content"android:padding="10dp"android:layout_margin="10dp"android:background="#FFF"style="@style/line_space"android:textColor="#000"android:text="第一行的文本\n第二行的文本(style设置的,行间距是默认行间距的1.5倍)"/><!--使用代码设置--><TextView
        android:id="@+id/id_tv_lineSpace4"android:layout_width="match_parent"android:layout_height="wrap_content"android:padding="10dp"android:layout_margin="10dp"android:background="#FFF"android:textColor="#000"android:text="第一行的文本\n第二行的文本(代码设置)"/></LinearLayout>
 <style name="line_space"><item name="android:lineSpacingMultiplier">1.5</item></style>

public class LineSpaceTextViewAct extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_line_space_text_view);TextView tv = (TextView)findViewById(R.id.id_tv_lineSpace4);tv.setLineSpacing(50,1.2f);}
}

setSpacing 方法:

  /*** Sets line spacing for this TextView.  Each line will have its height* multiplied by <code>mult</code> and have <code>add</code> added to it.** @attr ref android.R.styleable#TextView_lineSpacingExtra* @attr ref android.R.styleable#TextView_lineSpacingMultiplier*/public void setLineSpacing(float add, float mult) {if (mSpacingAdd != add || mSpacingMult != mult) {mSpacingAdd = add;mSpacingMult = mult;if (mLayout != null) {nullLayouts();requestLayout();invalidate();}}}

第一个参数相当于 android:lineSpacingExtra属性,第二个参数相当于android:lineSpaceingMultiplier属性。系统会采用哪个参数作为最终的行间距,取决于哪个参数值所表示的行间距大了。

这里写图片描述


在未显示完的文本后面加省略号(…)

当文本内容太多的时候,控件一行显示不开的时候,系统默认的会在最后显示一个省略号(…)

通过android:ellipsize属性可以设置省略号的位置,当属性值为none的时候则不显示省略号,默认在对后面加省略号。

需要设置 android:singleLine=”true”

代码设置如下:

textView.setEllipsize(TextUtils.TruncateAt.END);

xml报文设置:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:background="#000"android:orientation="vertical"><TextView
        android:id="@+id/id_tv_ignore1"android:layout_width="150dp"android:layout_height="wrap_content"android:background="#FFF"android:ellipsize="start"android:singleLine="true"android:layout_margin="10dp"android:text="维基百科的目标是建立拥有人类全部知识的百科全书" /><TextView
        android:id="@+id/id_tv_ignore2"android:layout_width="match_parent"android:layout_height="wrap_content"android:background="#FFF"android:ellipsize="middle"android:singleLine="true"android:layout_margin="10dp"android:text="维基百科的目标是建立拥有人类全部知识的百科全书dddddddddddddddddddddd" /><TextView
        android:id="@+id/id_tv_ignore3"android:layout_width="match_parent"android:layout_height="wrap_content"android:background="#FFF"android:ellipsize="end"android:singleLine="true"android:layout_margin="10dp"android:text="维基百科的目标是建立拥有人类全部知识的百科全书ddddddddddddddddddd" /></LinearLayout>

用TextView实现走马灯的效果

  1. TextView必须要获取焦点 ( android:focusable=”true”
    android:focusableInTouchMode=”true”)
    1. android:ellipsize=”marquee”
    2. android:marqueeRepeatLimit
    3. android:singleLine=”true”

android:marqueeRepeatLimit=“marquee_forever”或者是大于0的整数,marquee_forever表示永远循环显示。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:background="#000"><TextView
        android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="#FFF"android:ellipsize="marquee"android:focusable="true"android:focusableInTouchMode="true"android:marqueeRepeatLimit="marquee_forever"android:singleLine="true"android:text="李克勤(Hacken Lee),生于香港,籍贯广东新会崖西,中国香港歌手,演员,主持人" /></LinearLayout>

垂直滚动TextView中的文本(让TextView出现滚动条)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:background="#000"><TextView
        android:id="@+id/id_tv_scroll"android:layout_width="match_parent"android:layout_height="wrap_content"android:background="#FFF"android:singleLine="false"android:maxLines="4"android:scrollbars="vertical"android:scrollbarStyle="outsideOverlay"android:scrollbarFadeDuration="2000"android:padding="10dp"android:text="周杰伦, 生于中华民国台北县林口乡,是台湾著名国语流行音乐男歌手、演员、导演及音乐创作人。 周杰伦于2000年正式出道并发行其个人首张同名国语专辑《Jay》,翌年凭借该张专辑获得第十二届台湾金曲奖“最佳流行音乐演唱专辑奖”ddddddddddddddddddddddddddddddddddddddddddddddd。"/></RelativeLayout>

public class ScrollTextViewAct extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_scroll_text_view);TextView textView = (TextView) findViewById(R.id.id_tv_scroll);textView.setMovementMethod(ScrollingMovementMethod.getInstance());}
}

android:scrollbars=”vertical” : 垂直滚动必须为vertical
android:scrollbarStyle=”outsideOverlay” :滚动条在文字的右侧显示。如果insideOVerlay滚动条会在右侧文字上显示(会覆盖文字的一部分)。
android:scrollbarFadeDuration=”2000” 滚动条从出现到消失(以渐变的方式)的时间,单位是毫秒
这里写图片描述


http://chatgpt.dhexx.cn/article/gblcRpba.shtml

相关文章

Android TextWatcher监控EditText,TextView

布局中EditText在android布局中经常用到&#xff0c;对EditText中输入的内容也经常需要进行限制&#xff0c;我们可以通过TextWatcher去观察输入框中输入的内容&#xff0c;作个笔记。 主布局&#xff1a; <?xml version"1.0" encoding"utf-8"?>&…

使用TextWatcher监听EditText的文本变化之后动态改变EditText里面的内容

今天碰到一个这样的问题&#xff1a;使用TextWatcher对EditText进行监听&#xff0c;当EditText中值为某一特定内容时&#xff0c;将EditText中的值设为我们需要的那个值。一开始就是简单的在EditText中使用setText(...)来实现&#xff0c;结果出现栈溢出的异常。这里提供一个解…

详解EditText输入监听TextWatcher

日常开发中&#xff0c;我们可能会遇到需要监听EditText输入&#xff0c;比如判断输入是否为电话号码&#xff0c;获取输入的数据长度来限定字数等。这就需要监听EditText的输入状态。EditText使用TextWatcher实现类似按钮监听事件&#xff1a; 使用方法 效果图&#xff1a; …

Android TextWatcher三个回调详解,监听EditText的输入

TextWatcher是一个监听字符变化的类。当我们调用EditText的addTextChangedListener(TextWatcher)方法之后&#xff0c;就可以监听EditText的输入了。 在new出一个TextWatcher之后&#xff0c;我们需要实现三个抽象方法&#xff1a; beforeTextChangedonTextChangedafterTextCh…

android TextWatcher 学习

1.简介 主要用来监听用户输入&#xff0c;然后剪裁输入。 比如输入框只能输入8个字节的内容&#xff0c;就可以用TextWatcher来实现。 public interface TextWatcher extends NoCopySpan {/*** This method is called to notify you that, within <code>s</code>…

android的TextView的TextWatcher使用

TextWatcher是一个文本变化监听接口&#xff0c;定义了三个接口&#xff0c;分别是beforeTextChanged,onTextChanged,afterTextCahnged. TextWatcher通常与TextView结合使用&#xff0c;以便在文本变化的不同时机做响应的处理。TextWatcher中三个回调接口都是使用了InputFilter…

Android 文本监听接口TextWatcher详解

TextWatcher是一个用来监听文本变化的接口&#xff0c;使用该接口可以很方便的对可显示文本控件和可编辑文本控件中的文字进行监听和修改 TextWatcher接口中定义了三个方法&#xff1a; public void beforeTextChanged(CharSequence s, int start, int count, int after) {} 该…

makefile中的两个函数(wildcard和patsubst)

(1) wildcard函数 作用是查找指定目录下指定类型的文件&#xff0c;并最终返回一个环境变量&#xff0c;需要用$取值赋值给另一个环境变量&#xff01;该函数只有一个参数&#xff0c;如取出当前目录下的所有.c文件&#xff0c;并赋值给allc普通变量&#xff1a; allc$(wildc…

linux_makefile文件编写,基本规则、工作原理、模式规则,wildcard函数、patsubst函数

接上一篇&#xff1a;linux_GDB调试学习(调试运行、多文件设置断点)_C/C程序调试 本次来分享linux下的makefile文件的编写&#xff0c;开始上菜&#xff1a; 目录 1.makefile文件的命名规则2.用途3.基本规则3.1.用例一4.工作原理4.1.用例二 5.makefile的执行5.1.用例三 6.make…

makefile wildcard patsubst使用小结

这是当前的目录&#xff1a; objs main.o g_a.o g_b. target : $(objs)gcc -o target $(objs) main.o : g_a.h g_b.hgcc -c main.c g_a.o : g_a.hgcc -c g_a.c g_b.o : g_b.hgcc -c g_b.c.PHONY : cleansrc $(wildcard *.c) obj $(patsubst %.c,%.o,$(src)) #obj $(patsu…

Makefile中patsubst函数使用方法

Makefile中patsubst函数使用方法 patsubst函数用于将文件模式进行替换。 一、作用 替换文件后缀。 二、格式 $(patsubst 原模式&#xff0c; 目标模式&#xff0c; 文件列表) 三、实例 图1 源文件结构 图2 patsubst实例

从零开始学习makefile(5)makefile中patsubst的作用

目录 介绍 text pattern与replacement 返回值 通配符% 示例1 例子2 介绍 patsubst是pattern substitute的缩写。其用法是&#xff1a; $(patsubst pattern,replacement,text) text text是将要被处理的字符串。首先&#xff0c;patsubst以空格为分隔符&#xff0c;将te…

list_for_each_entry和list_for_each_entry_safe

看内核代码都会发现&#xff0c;内核链表的操作常用的二个宏list_for_each_entry和list_for_each_entry_safe 循序渐进&#xff0c;先从最底层的函数container_of函数说起&#xff0c;其内核定义如下&#xff1a; 先看offsetof宏&#xff0c;根据优先级的顺序&#xff0c;最里面…

Map中的entry,entrySet,keySet的区别和用法

Map中的元素是以键值对的形式存在的&#xff0c;即key-value。key是唯一的不能重复&#xff0c;但value可以重复。 Map.keySet(): 这个方法返回的就是map集合中所有键Key的一个Set集合。如Map<Integer&#xff0c;String> 中put(1, “张三”)&#xff0c;put(2, “李四”…

Map.Entry与entrySet与entry,getKey()与entry.getValue()的用法

直接上代码 实体类 Data AllArgsConstructor NoArgsConstructor public class SinglePressureResultDTO {private Integer Times; private Integer SCU_number; private Boolean Intervention; private Long startTime_low; private Long low_time; private Long start…

Map集合的entrySet()方法

之前学习集合的时候要通过迭代器来迭代的时候最难得就是map集合得迭代&#xff0c;一直也不太明白&#xff0c;今天总算搞懂了 首先我们看什么容器才能迭代 根据API我们得知是对所有collection迭代的集合&#xff0c;那么已知的collection容器有哪些 我把常用的标出了&#xf…

keySet()和entrySet()

一、描述 keySet()和entrySet()&#xff0c;是Map集合中的两种取值方法。 与get(Object key)相比&#xff0c;get(Object key)只能返回到指定键所映射的值&#xff0c;不能一次全部取出。而keySet()和entrySet()可以。 Map集合中没有迭代器&#xff0c;Map集合取出键值的原理…

Java高级之HashMap中的entrySet()方法

基本使用 entrySet()方法得到HashMap中各个键值对映射关系的集合。 然后Map.Entry中包含了getKey()和getValue()方法获取键和值。 示例&#xff1a; public class Demo {public static void main(String[] args) {Map<String, String> map new HashMap<>();ma…

KeySet和EntrySet区别

场景&#xff1a; keySet()和entrySet()&#xff0c;是Map集合中的两种取值方法。 与get(Object key)相比&#xff0c;get(Object key)只能返回到指定键所映射的值&#xff0c;不能一次全部取出。而keySet()和entrySet()可以。 Map集合中没有迭代器&#xff0c;Map集合取出键…

Map中entrySet()方法使用

public Set<Map.Entry<K,V>> entrySet(): 获取到Map集合中所有的键值对对象的集合(Set集合)。 就是返回一个集合&#xff0c;集合里存放的是对象&#xff0c;创建对象的类有两个属性&#xff0c;分别是 键和值 也即键值对。 其中Entry是属于Map的静态内部类&#x…