怎么在TextView中的部分文字上响应点击事件
推荐于2016-06-13
展开全部
package android.alex.clickabletext;
import android.app.Activity;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
public class ClickableText extends Activity {
/** Called when the activity is first created. */
private TextView mClickableText;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout layout = new LinearLayout(this);
mClickableText = new TextView(this);
mClickableText.setClickable(true);
mClickableText.setTextSize(20);
layout.addView(mClickableText);
setContentView(layout);
mClickableText.setText(getClickableSpan());
mClickableText.setMovementMethod(LinkMovementMethod.getInstance());
}
private SpannableString getClickableSpan()
{
View.OnClickListener l = new View.OnClickListener(){
@Override
public void onClick(View v)
{
Toast.makeText(ClickableText.this, "Click Success", Toast.LENGTH_SHORT).show();
}
};
SpannableString spanableInfo = new SpannableString("This is a test, Click Me");
int start = 16;
int end = spanableInfo.length();
spanableInfo.setSpan(new Clickable(l), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
return spanableInfo;
}
}
class Clickable extends ClickableSpan implements OnClickListener{
private final View.OnClickListener mListener;
public Clickable(View.OnClickListener l){
mListener = l;
}
@Override
public void onClick(View v){
mListener.onClick(v);
}
}
import android.app.Activity;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
public class ClickableText extends Activity {
/** Called when the activity is first created. */
private TextView mClickableText;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout layout = new LinearLayout(this);
mClickableText = new TextView(this);
mClickableText.setClickable(true);
mClickableText.setTextSize(20);
layout.addView(mClickableText);
setContentView(layout);
mClickableText.setText(getClickableSpan());
mClickableText.setMovementMethod(LinkMovementMethod.getInstance());
}
private SpannableString getClickableSpan()
{
View.OnClickListener l = new View.OnClickListener(){
@Override
public void onClick(View v)
{
Toast.makeText(ClickableText.this, "Click Success", Toast.LENGTH_SHORT).show();
}
};
SpannableString spanableInfo = new SpannableString("This is a test, Click Me");
int start = 16;
int end = spanableInfo.length();
spanableInfo.setSpan(new Clickable(l), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
return spanableInfo;
}
}
class Clickable extends ClickableSpan implements OnClickListener{
private final View.OnClickListener mListener;
public Clickable(View.OnClickListener l){
mListener = l;
}
@Override
public void onClick(View v){
mListener.onClick(v);
}
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询