SoFunction
Updated on 2025-03-01

Android TextView Two ways to set and cancel strikethrough

1. There are two ways to set the strikethrough in TextView:

(Recommended) Method 1:

Set the TextView's original Flags property and strikethrough by bitwise or operator|. The TextView will be redrawn in setPaintFlags.

(() | Paint.STRIKE_THRU_TEXT_FLAG);

Method 2:

After obtaining the brush, set the properties and redraw the TextView. There is a problem with this method, which will replace the original Flags attribute of TextView, such as anti-aliasing, etc. If you look closely, you will find that in this way, the text has jagged teeth.

().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
();

2. TextView has two ways to cancel the strikethrough:

(Recommended) Method 1:

First, the Paint.STRIKE_THRU_TEXT_FLAG attribute is inverted, and then the bitwise and operator& is used to remove the delete line attribute and retain the TextView's original Flags attribute. The TextView will be redrawn in setPaintFlags.

(() & (~Paint.STRIKE_THRU_TEXT_FLAG));

Method 2:

After obtaining the brush, clear the Flags property and then redraw the TextView. There is a problem with this method, which will clear all the original Flags properties of the TextView, such as anti-aliasing, etc. If you look closely, you will find that in this way, the text has jagged teeth;

().setFlags(0);
();

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.