如何在Textview android中单击高亮单词
作者:互联网
我正在创建一个应用程序,我在EditText中从用户那里获得输入.
现在,在单击按钮后,输入的字符串将显示在TextView中.现在我的问题是,当用户点击任何一个单词时,我会在TextView中突出显示该单词.
例如:用户输入:你好朋友你好吗?
当用户单击“Hello”时,它将在TextView中突出显示.或者,当用户点击“如何”时,它将突出显示.
请帮我.
解决方法:
public class MainActivity extends Activity {
TextView _tv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
_tv = (TextView) findViewById( R.id.tv );
SpannableString ss = new SpannableString("Hello friend how are you.");
ss.setSpan(new MyClickableSpan(), 0,5, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss.setSpan(new MyClickableSpan(), 6, 12, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss.setSpan(new MyClickableSpan(), 13, 16, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss.setSpan(new MyClickableSpan(), 17, 20, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss.setSpan(new MyClickableSpan(), 21, 24, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
_tv.setText(ss);
_tv.setMovementMethod(LinkMovementMethod.getInstance());
}
class MyClickableSpan extends ClickableSpan{ //clickable span
public void onClick(View textView) {
//do something
Toast.makeText(MainActivity.this, "Clicked",
Toast.LENGTH_SHORT).show();
}
@Override
public void updateDrawState(TextPaint ds) {
ds.setColor(Color.BLACK);//set text color
ds.setUnderlineText(false); // set to false to remove underline
}
}
}
编辑:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="50dp"
android:text="@string/hello_world" />
<EditText
android:id="@+id/ed"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_marginTop="40dp" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:text="Click" />
</LinearLayout>
您可以按空格分割字符串,并将每个单词设置为spannable字符串.然后使spannable字符串可单击.这样您就不需要对String的长度进行硬编码.用户可以单击textview中的任何单词并突出显示.
public class MainActivity extends Activity {
TextView _tv;
String[] each;
SpannableString ss1;
Button b;
EditText et;
String s;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b= (Button) findViewById(R.id.button1);
et = (EditText) findViewById(R.id.ed);
_tv = (TextView) findViewById( R.id.tv );
b.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
s=et.getText().toString();
_tv.setText("");
for(int i=0;i<s.length();i++)
{
each = s.split("\\s+");
}
for(int i=0;i<each.length;i++)
{
System.out.println("................"+each[i]);
ss1= new SpannableString(each[i]);
ss1.setSpan(new MyClickableSpan(), 0, ss1.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
_tv.append(ss1);
_tv.append(" ");
}
_tv.setMovementMethod(LinkMovementMethod.getInstance());
}
});
}
class MyClickableSpan extends ClickableSpan{ //clickable span
public void onClick(View textView) {
//do something
Toast.makeText(MainActivity.this, "Clicked",
Toast.LENGTH_SHORT).show();
}
@Override
public void updateDrawState(TextPaint ds) {
ds.setColor(Color.BLACK);//set text color
ds.setUnderlineText(false); // set to false to remove underline
}
}
}
http://www.chrisumbel.com/article/android_textview_rich_text_spannablestring.
用于造型可扩展的字符串.
结果快照.
标签:android,android-textview 来源: https://codeday.me/bug/20190729/1568451.html