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
}
}
}
编辑:
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
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" />
android:id="@+id/ed"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_marginTop="40dp" />
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" />
您可以按空格分割字符串,并将每个单词设置为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
{
each = s.split("\\s+");
}
for(int i=0;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
}
}
}
结果快照.