删除 Android 中的油漆标志

我的代码是这样的:

    TextView task_text = (TextView) view.findViewById(R.id.task_text);
task_text.setPaintFlags( task_text.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);

这会导致文本上出现一个删除通过效果。但是,我想知道如何删除一旦设置的标志,以及如何检测的标志设置。

我知道这是一个位操作,但是我已经试过了 ~ 和-操作符,两种都没用。

37372 次浏览

To remove a flag, this should work:

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

Which means set all the set flags, except of Paint.STRIKE_THRU_TEXT_FLAG.

To check if a flag is set (Edit: for a moment I forgot it is java...):

if ((task_text.getPaintFlags() & Paint.STRIKE_THRU_TEXT_FLAG) > 0)

This also works:

task_text.setPaintFlags(0);

Use exclusive OR operator ^ instead of | with &(~) combination:

// setup STRIKE_THRU_TEXT_FLAG flag if current flags not contains it
task_text.setPaintFlags(task_text.getPaintFlags() ^ Paint.STRIKE_THRU_TEXT_FLAG));


// second call will remove STRIKE_THRU_TEXT_FLAG
task_text.setPaintFlags(task_text.getPaintFlags() ^ Paint.STRIKE_THRU_TEXT_FLAG));

Check if flag is currently setup:

if((task_text.getPaintFlags() & Paint.STRIKE_THRU_TEXT_FLAG) == Paint.STRIKE_THRU_TEXT_FLAG)

|--------------------------------------------------|
|<*>| Underline with a textView :
|--------------------------------------------------|

|*| Add Underline :

 txtVyuVar.setPaintFlags(txtVyuVar.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);

|*| Remove Underline :

txtVyuVar.setPaintFlags(txtVyuVar.getPaintFlags() ^ Paint.UNDERLINE_TEXT_FLAG);

|*| Check Underline :

if((txtVyuVar.getPaintFlags() & Paint.UNDERLINE_TEXT_FLAG) == Paint.UNDERLINE_TEXT_FLAG)
{
// Codo Todo
}

|*| Toggle Underline :

if((txtVyuVar.getPaintFlags() & Paint.UNDERLINE_TEXT_FLAG) == Paint.UNDERLINE_TEXT_FLAG)
{
txtVyuVar.setPaintFlags(txtVyuVar.getPaintFlags() ^ Paint.UNDERLINE_TEXT_FLAG);
}
else
{
txtVyuVar.setPaintFlags(txtVyuVar.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
}

In my opinion, just set its default flag is a better choice. Otherwise, the text will be looked jagged. The default flag in TextView (EditText extends TextView) is

Paint.ANTI_ALIAS_FLAG

And set a new paintflag will replace the previous one. I have made a test to verify it. So, just like this:

task_text.setPaintFlags(Paint.ANTI_ALIAS_FLAG);

In Kotlin

task_text.paintFlags = task_text.paintFlags and Paint.STRIKE_THRU_TEXT_FLAG.inv()

TextView details_actual_price; //and find id from your xml file

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

//used for adapter calss

Textview details_actual_price; // and find id holder.actulPrice.setPaintFlags(holder.actulPrice.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);

I tried using the above mentioned methods in kotlin using kotlin bitwise operators and I was getting weird results...

so to remove a flag I just did

view.paintflags = 0x00

In Kotlin: numberTextView?.paintFlags = Paint.STRIKE_THRU_TEXT_FLAG to remove numberTextView?.paintFlags = Paint.STRIKE_THRU_TEXT_FLAG and Paint.STRIKE_THRU_TEXT_FLAG.inv()