【发布时间】:2018-05-09 05:42:22
【问题描述】:
是否可以在Android中将EditText的选定样式设置为删除线。 有人可以给我看示例代码吗?
【问题讨论】:
-
那是 textview 的对吧?
-
是的,还要解释内容/答案。要显示它,您需要使用 RichText 显示,EditText 不支持 RichText。
标签: android
是否可以在Android中将EditText的选定样式设置为删除线。 有人可以给我看示例代码吗?
【问题讨论】:
标签: android
试试这个
public class MainActivity extends AppCompatActivity {
EditText editText;
@SuppressLint("ClickableViewAccessibility")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = findViewById(R.id.search_edit_frame);
editText.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (MotionEvent.ACTION_UP == event.getAction()) {
if (editText.hasSelection()) {
if(!TextUtils.isEmpty(editText.getText().toString())){
final int selStart = editText.getSelectionStart();
final int selEnd = editText.getSelectionEnd();
SpannableString spannableString = new SpannableString(editText.getText().toString());
spannableString.setSpan(new StrikethroughSpan(), selStart, selEnd, 0);
editText.setText(spannableString);
return false;
}
}
}
return false;
}
});
}
}
布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/rotate">
<EditText
android:id="@+id/search_edit_frame"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginTop="10dp"
android:text="Nilesh Rathod"
android:focusable="true"
android:focusableInTouchMode="true"
android:textColor="@color/colorPrimaryDark" />
</LinearLayout>
【讨论】:
如何在 Android 中以编程方式删除 TextView 文本
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/rl" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="16dp" tools:context=".MainActivity" android:background="@android:color/white" <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Sample TextView...\nSecond line of TextView" android:padding="15dp" android:textSize="25dp" android:textColor="#ff9c308c" /> <Button android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Set Text Strike Through" android:layout_below="@id/tv" /> </RelativeLayout>MainActivity.java
package com.cfsuman.me.androidcodesnippets; import android.graphics.Paint; import android.os.Bundle; import android.app.Activity; import android.view.View; import android.widget.Button; import android.widget.RelativeLayout; import android.widget.TextView; public class MainActivity extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Get the widgets reference from XML layout RelativeLayout rl = (RelativeLayout) findViewById(R.id.rl); final TextView tv = (TextView) findViewById(R.id.tv); Button btn = (Button) findViewById(R.id.btn); // Set a click listener for Button widget btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { /* public int getPaintFlags () Returns the flags on the Paint being used to display the text. setPaintFlags(int flags) Sets flags on the Paint being used to display the text and reflows the text if they are different from the old flags. STRIKE_THRU_TEXT_FLAG Paint flag that applies a strike-through decoration to drawn text. */ // Set TextView text strike through tv.setPaintFlags(tv.getPaintFlags()| Paint.STRIKE_THRU_TEXT_FLAG); } }); } }
发件人:https://android--code.blogspot.com/2015/05/android-textview-strikethrough.html
【讨论】:
tv.setPaintFlags(tv.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
TextView,而不是 OP 要求的 EditText。
只需为 editText 设置绘制标志即可。
editText.paintFlags = Paint.STRIKE_THRU_TEXT_FLAG
要删除集合,
editText.paintFlags = Paint.CURSOR_AFTER
【讨论】: