有没有一种简单的方法可以删除应用程序小部件中的文本?

发布于 2024-09-26 08:03:02 字数 247 浏览 1 评论 0原文

我想知道是否有一种简单的方法可以在 Android 的应用程序小部件中删除文本。在正常活动中,使用文本视图标志非常简单:

textView.setPaintFlags(textView.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);

但是由于在应用程序小部件中,我只能使用远程视图...我不知道这是否可能

有人知道这件事吗?

谢谢!

I was wondering if there is an easy way to strike text within an app widget in Android. In a normal activity, it is pretty easy, using textview flags:

textView.setPaintFlags(textView.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);

But since in an app widget, I can use only remoteviews... I do not know if this is possible

Anyone know something about this?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(23

唠甜嗑 2024-10-03 08:03:03

另一种以编程方式完成此操作的方法,与 Paint 方法相比,它看起来不像黑客:

而不是这样做:

tv.setText(s);

做:

private static final StrikethroughSpan STRIKE_THROUGH_SPAN = new StrikethroughSpan();
...
tv.setText(s, TextView.BufferType.SPANNABLE);
Spannable spannable = (Spannable) tv.getText();
spannable.setSpan(STRIKE_THROUGH_SPAN, 0, s.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

Another way to do it programmatically which looks a bit less like a hack than the Paint way:

Instead of doing:

tv.setText(s);

do:

private static final StrikethroughSpan STRIKE_THROUGH_SPAN = new StrikethroughSpan();
...
tv.setText(s, TextView.BufferType.SPANNABLE);
Spannable spannable = (Spannable) tv.getText();
spannable.setSpan(STRIKE_THROUGH_SPAN, 0, s.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
空心空情空意 2024-10-03 08:03:03

您可以使用这个:

remoteviews.setInt(R.id.YourTextView, "setPaintFlags", Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);

当然您也可以从 android.graphics.Paint 类添加其他标志。

You can use this:

remoteviews.setInt(R.id.YourTextView, "setPaintFlags", Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);

Of course you can also add other flags from the android.graphics.Paint class.

日裸衫吸 2024-10-03 08:03:03

2015 更新:各位,这是针对非常旧版本的 Android 的。查看现代解决方案的其他答案!


要删除整个文本视图,您可以使用特定的背景图像来模拟删除效果:

android:background="@drawable/bg_strikethrough"

其中 bg_strikethrough 可绘制对象是一个 9 块,它保持一条实线穿过中间,并在两侧增长,无论您认为合理多少填充。我用过这样的一个:

替代文字

(为了清晰起见,放大了.. 1300%!)

替代文字

这是我的 HDPI 版本,因此保存它(第一个 https://i.sstatic.net/nt6BK.png) 作为 res/drawable-hdpi/bg_strikethrough.9.png 并且配置将按如下方式工作:

替代文字

2015 Update: Folks, this is for very old versions of Android. See other answers for modern solutions!


To strike through the entire text view, you can use a specific background image to simulate the strikethrough effect:

android:background="@drawable/bg_strikethrough"

Where the bg_strikethrough drawable is a 9-patch that keeps a solid line through the middle, growing either side, with however much padding you think is reasonable. I've used one like this:

alt text

(enlarged for clarity.. 1300% !)

alt text

That is my HDPI version, so save it (the first one https://i.sstatic.net/nt6BK.png) as res/drawable-hdpi/bg_strikethrough.9.png and the configuration will work as so:

alt text

暖伴 2024-10-03 08:03:03

为此,您可以使用它

 title.setPaintFlags(title.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);

,如果要删除,您可以使用它

 title.setPaintFlags(title.getPaintFlags() & (~Paint.STRIKE_THRU_TEXT_FLAG));

For doing this you can use this

 title.setPaintFlags(title.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);

and for remove you can use this

 title.setPaintFlags(title.getPaintFlags() & (~Paint.STRIKE_THRU_TEXT_FLAG));
夜无邪 2024-10-03 08:03:03

这是为所有 Kotlin 人员提供的扩展

fun TextView.showStrikeThrough(show: Boolean) {
    paintFlags =
            if (show) paintFlags or Paint.STRIKE_THRU_TEXT_FLAG
            else paintFlags and Paint.STRIKE_THRU_TEXT_FLAG.inv()
}

用法

textView.showStrikeThrough(true)

限制

删除线只能与文本颜色相同

(即红色文本和蓝色删除线是不可能的)< /em>

Here is an extension for all you Kotlin folks

fun TextView.showStrikeThrough(show: Boolean) {
    paintFlags =
            if (show) paintFlags or Paint.STRIKE_THRU_TEXT_FLAG
            else paintFlags and Paint.STRIKE_THRU_TEXT_FLAG.inv()
}

Usage

textView.showStrikeThrough(true)

Limitation

Strikethroughs can only be the same color as the text

(i.e. Red text and blue strikethrough is not possible)

温柔戏命师 2024-10-03 08:03:03

制作一个可绘制的文件,
striking_text.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="false">
        <shape android:shape="line">
            <stroke android:width="1dp" android:color="@android:color/holo_red_dark" />
        </shape>
    </item>
</selector>

布局 xml

 <TextView
            ....
            ....
            android:background="@drawable/striking_text"
            android:foreground="@drawable/striking_text"
            android:text="69$"
           />

如果您的最低 SDK 低于 API 级别 23,则只需使用背景
只需将背景和前景放在 Textview 中,然后 android studio 就会显示一条错误,指出为 API >23 创建一个布局文件,然后删除 android:foreground="@drawable /stricking_text" 从主布局

输出看起来像这样:
输入图像描述这里

Make a drawable file,
striking_text.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="false">
        <shape android:shape="line">
            <stroke android:width="1dp" android:color="@android:color/holo_red_dark" />
        </shape>
    </item>
</selector>

layout xml

 <TextView
            ....
            ....
            android:background="@drawable/striking_text"
            android:foreground="@drawable/striking_text"
            android:text="69
quot;
           />

If your min SDK below API level 23 just use the background
or just put the background and foreground in the Textview then the android studio will show an error that says create a layout file for API >23 after that remove the android:foreground="@drawable/stricking_text" from the main layout

Output look like this:
enter image description here

梦回旧景 2024-10-03 08:03:03

如果您使用字符串,这真的很容易:

<string name="line"> Not crossed <strike> crossed </strike> </string>

然后只需:

<TextView 
        ...
         android:text="@string/line"
 />

It is really easy if you are using strings:

<string name="line"> Not crossed <strike> crossed </strike> </string>

And then just:

<TextView 
        ...
         android:text="@string/line"
 />
眉目亦如画i 2024-10-03 08:03:03

对于多行 TextView,您应该使用 android.text.style.CharacterStyle,如下所示:

SpannableString spannable = new SpannableString(text);
spannable.setSpan(new StrikethroughSpan(), 0, text.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
remoteViews.setTextViewText(R.id.itemText, spannable);

For multiline TextView you should use android.text.style.CharacterStyle like this:

SpannableString spannable = new SpannableString(text);
spannable.setSpan(new StrikethroughSpan(), 0, text.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
remoteViews.setTextViewText(R.id.itemText, spannable);
猫性小仙女 2024-10-03 08:03:03

添加以下行:-

TextView tv=(TextView) v.findViewById(android.R.id.text1);
tv.setPaintFlags(tv.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);

使用您的参考而不是“电视”

Add the line below:-

TextView tv=(TextView) v.findViewById(android.R.id.text1);
tv.setPaintFlags(tv.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);

use your reference instead of "tv"

尸血腥色 2024-10-03 08:03:03

试试这个代码

textview.setText((Html.fromHtml("<strike>hello world!</strike>")));

try this code

textview.setText((Html.fromHtml("<strike>hello world!</strike>")));
清秋悲枫 2024-10-03 08:03:03

科特林方式

val tv = findViewById<View>(R.id.mytext) as TextView
tv.text = "This is strike-through"
tv.paintFlags = tv.paintFlags or Paint.STRIKE_THRU_TEXT_FLAG

Kotlin way

val tv = findViewById<View>(R.id.mytext) as TextView
tv.text = "This is strike-through"
tv.paintFlags = tv.paintFlags or Paint.STRIKE_THRU_TEXT_FLAG
胡渣熟男 2024-10-03 08:03:03

Android 资源具有非常好的 HTML 标记支持
支持以下 HTML 元素:

Bold: <b>, <em>
Italic: <i>, <cite>, <dfn>
25% larger text: <big>
20% smaller text: <small>
Setting font properties: <font face=”font_family“ color=”hex_color”>. Examples of possible font families include monospace, serif, and sans_serif.
Setting a monospace font family: <tt>
Strikethrough: <s>, <strike>, <del>
Underline: <u>
Superscript: <sup>
Subscript: <sub>
Bullet points: <ul>, <li>
Line breaks: <br>
Division: <div>
CSS style: <span style=”color|background_color|text-decoration”>
Paragraphs: <p dir=”rtl | ltr” style=”…”>

但请注意,它不会在 android studio 布局预览中呈现。上次在 Android Studio 3.3.1 上进行测试

例如,删除线将如下所示:

<string name="cost"><strike>$10</strike> $5 a month</string>

Android resources have pretty good HTML markup support
The below HTML elements are supported:

Bold: <b>, <em>
Italic: <i>, <cite>, <dfn>
25% larger text: <big>
20% smaller text: <small>
Setting font properties: <font face=”font_family“ color=”hex_color”>. Examples of possible font families include monospace, serif, and sans_serif.
Setting a monospace font family: <tt>
Strikethrough: <s>, <strike>, <del>
Underline: <u>
Superscript: <sup>
Subscript: <sub>
Bullet points: <ul>, <li>
Line breaks: <br>
Division: <div>
CSS style: <span style=”color|background_color|text-decoration”>
Paragraphs: <p dir=”rtl | ltr” style=”…”>

Note however that it's not rendered in android studio layouts preview. Last tested on Android Studio 3.3.1

For example, the Strikethrough will look like that:

<string name="cost"><strike>$10</strike> $5 a month</string>
绮筵 2024-10-03 08:03:03

适用于棒棒糖及以上版本。创建一个可绘制对象

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="false">
        <shape android:shape="line">
            <stroke android:width="1dp"
                android:color="@color/onePlusRed" />
        </shape>
    </item>
</selector>

并将其用作前景。
android:foreground="@drawable/strike_through"

For Lollipop and above. create a drawable

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="false">
        <shape android:shape="line">
            <stroke android:width="1dp"
                android:color="@color/onePlusRed" />
        </shape>
    </item>
</selector>

and use it as foreground.
android:foreground="@drawable/strike_through"

客…行舟 2024-10-03 08:03:03

Kotlin方式

averagePrice.paintFlags = Paint.STRIKE_THRU_TEXT_FLAG

Kotlin way

averagePrice.paintFlags = Paint.STRIKE_THRU_TEXT_FLAG

昵称有卵用 2024-10-03 08:03:03

您可以使用数据绑定!
在 XML 布局中只需执行 app:strike="@{true}"

@BindingAdapter("strike")
fun bindTextView(view: TextView, strike: Boolean) {
    if (strike) {
        view.paintFlags = view.paintFlags or Paint.STRIKE_THRU_TEXT_FLAG or Paint.ANTI_ALIAS_FLAG
    } else {
        view.paintFlags = 0 or Paint.ANTI_ALIAS_FLAG
    }
}

You Can use data binding!
In XML layout just do app:strike="@{true}"

@BindingAdapter("strike")
fun bindTextView(view: TextView, strike: Boolean) {
    if (strike) {
        view.paintFlags = view.paintFlags or Paint.STRIKE_THRU_TEXT_FLAG or Paint.ANTI_ALIAS_FLAG
    } else {
        view.paintFlags = 0 or Paint.ANTI_ALIAS_FLAG
    }
}

您的好友蓝忘机已上羡 2024-10-03 08:03:03

-创建一个绑定适配器类并

@BindingAdapter("strikeThrough")
    @JvmStatic
    fun strikeThrough(textView: MaterialTextView, strikeThrough: Boolean) {
        if (strikeThrough) {
            textView.paintFlags = textView.paintFlags or Paint.STRIKE_THRU_TEXT_FLAG
        } else {
            textView.paintFlags = textView.paintFlags and Paint.STRIKE_THRU_TEXT_FLAG.inv()
        }
    }

在 xml 上添加此方法,您必须设置此方法

        <com.google.android.material.textview.MaterialTextView
            android:id="@+id/txtFormattedFinalPrice"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/text_color_discount"
            android:textSize="@dimen/_8ssp"
            app:strikeThrough="@{true}"
            app:layout_constraintStart_toStartOf="parent"
          />

-create a binding adapter class and add this method

@BindingAdapter("strikeThrough")
    @JvmStatic
    fun strikeThrough(textView: MaterialTextView, strikeThrough: Boolean) {
        if (strikeThrough) {
            textView.paintFlags = textView.paintFlags or Paint.STRIKE_THRU_TEXT_FLAG
        } else {
            textView.paintFlags = textView.paintFlags and Paint.STRIKE_THRU_TEXT_FLAG.inv()
        }
    }

on xml u have to set this

        <com.google.android.material.textview.MaterialTextView
            android:id="@+id/txtFormattedFinalPrice"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/text_color_discount"
            android:textSize="@dimen/_8ssp"
            app:strikeThrough="@{true}"
            app:layout_constraintStart_toStartOf="parent"
          />
少女净妖师 2024-10-03 08:03:03

您添加:

TextView variableTv = (TextView) findViewById(R.id.yourText);

您设置/添加您变量:

variableTv.setText("It's Text use Style Strike");

然后在variableTv中添加.setPaintFlags:

variableTv.setPaintFlags(variableTv.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);

you add in :

TextView variableTv = (TextView) findViewById(R.id.yourText);

you set/add in You variable :

variableTv.setText("It's Text use Style Strike");

and then add .setPaintFlags in variableTv :

variableTv.setPaintFlags(variableTv.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
红尘作伴 2024-10-03 08:03:03

我尝试了几种选择,但是,这最适合我:

String text = "<strike><font color=\'#757575\'>Some text</font></strike>";
textview.setText(Html.fromHtml(text));

干杯

I tried few options but, this works best for me:

String text = "<strike><font color=\'#757575\'>Some text</font></strike>";
textview.setText(Html.fromHtml(text));

cheers

ぃ弥猫深巷。 2024-10-03 08:03:03

作为 Kotlin 扩展的另一个解决方案:

var TextView.isStrikeThrough: Boolean
    get() = (paintFlags and Paint.STRIKE_THRU_TEXT_FLAG) == Paint.STRIKE_THRU_TEXT_FLAG
    set(value) {
        paintFlags = if (value) {
            paintFlags or Paint.STRIKE_THRU_TEXT_FLAG
        } else {
            paintFlags and Paint.STRIKE_THRU_TEXT_FLAG.inv()
        }
    }

Another solution as a Kotlin extension:

var TextView.isStrikeThrough: Boolean
    get() = (paintFlags and Paint.STRIKE_THRU_TEXT_FLAG) == Paint.STRIKE_THRU_TEXT_FLAG
    set(value) {
        paintFlags = if (value) {
            paintFlags or Paint.STRIKE_THRU_TEXT_FLAG
        } else {
            paintFlags and Paint.STRIKE_THRU_TEXT_FLAG.inv()
        }
    }

思念绕指尖 2024-10-03 08:03:03

我创建了这个方法,可以从我的应用程序中的任何位置使用,

static String formatSellingPrice(Context ctx, String Original_Price, String Selling_Price, boolean isUnder_Promotion) {
        String Currency_Symbol = ctx.getResources().getString(R.string.Currency_Symbol);

        if (isUnder_Promotion) {
            return String.format("<strike><font color=\"#757575\">%s %s</font></strike> %s %s", Original_Price, Currency_Symbol, Selling_Price, Currency_Symbol);
        } else {
            return Selling_Price + " " + Currency_Symbol;
        }
    }

如果您有多语言和多货币应用程序,

我可以这样称呼它:

String formated_SellingPrice = formatSellingPrice(this, Original_Price, Selling_Price, isUnder_Promotion);

然后像这样分配

tv_Card_Selling_Price.setText(Html.fromHtml(formated_SellingPrice));

I created this method to use from any where within my app

static String formatSellingPrice(Context ctx, String Original_Price, String Selling_Price, boolean isUnder_Promotion) {
        String Currency_Symbol = ctx.getResources().getString(R.string.Currency_Symbol);

        if (isUnder_Promotion) {
            return String.format("<strike><font color=\"#757575\">%s %s</font></strike> %s %s", Original_Price, Currency_Symbol, Selling_Price, Currency_Symbol);
        } else {
            return Selling_Price + " " + Currency_Symbol;
        }
    }

it can be used if you have multi language and multi currency app

I call it like this:

String formated_SellingPrice = formatSellingPrice(this, Original_Price, Selling_Price, isUnder_Promotion);

then assign like this

tv_Card_Selling_Price.setText(Html.fromHtml(formated_SellingPrice));
以酷 2024-10-03 08:03:03

我已经在常规(本地)TextView 上完成了此操作,并且它应该适用于远程品种,因为文档列出了两者之间等效的方法:

remote_text_view.setText(Html.fromHtml (“这是划掉了。”));

I've done this on a regular (local) TextView, and it should work on the remote variety since the docs list the method as equivalent between the two:

remote_text_view.setText(Html.fromHtml("This is <del>crossed off</del>."));

自演自醉 2024-10-03 08:03:03

我知道,我正在回答一个老问题,它可能对其他人有帮助,可以以编程方式删除 TextView 的特定部分。

TextView textView = (TextView) findViewById(R.id.textView);
textView.setText("Text need to be set here", TextView.BufferType.SPANNABLE);
Spannable spannable = (Spannable) textView.getText();
spannable.setSpan(new StrikethroughSpan(), 5, 8, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);

所以,在这个示例代码中,5是需要删除的文本的起始位置,8是需要删除的文本的结束位置,因此运行此代码后我们可以得到删除掉的文本“need”。希望它对其他人有帮助。

I know, I am answering an old question, it might be helpful for someone else, for striking out a particular portion of TextView programmatically.

TextView textView = (TextView) findViewById(R.id.textView);
textView.setText("Text need to be set here", TextView.BufferType.SPANNABLE);
Spannable spannable = (Spannable) textView.getText();
spannable.setSpan(new StrikethroughSpan(), 5, 8, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);

So, in this example code, 5 is starting position and 8 is ending position of text need to be stricken out, so after running this code we can get the text "need" with striked out. Hope it will helpful for someone else.

没有你我更好 2024-10-03 08:03:02

要在文本视图中以编程方式执行此操作,未在其他视图中进行测试>>

TextView tv = (TextView) findViewById(R.id.mytext);
tv.setText("This is strike-thru");
tv.setPaintFlags(tv.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);

To do it programatically in a textview, untested in other views >>

TextView tv = (TextView) findViewById(R.id.mytext);
tv.setText("This is strike-thru");
tv.setPaintFlags(tv.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文