2016-11-05 66 views
0

打開新的另一個活動我想用startActivity(new Intent(context, MyProfile.class));當與#或@如何使用ClickableSpan

這裏TextView的用戶類C是我的代碼

class CalloutLinkProfile extends ClickableSpan { 

    Context context; 
    TextPaint textPaint; 

    public CalloutLinkProfile(Context ctx) { 
     super(); 
     context = ctx; 

    } 


    @Override 
    public void updateDrawState(TextPaint ds) { 
     textPaint = ds; 
     // ds.setColor(0xff336699); 

     ds.setARGB(255, 30, 144, 255); 
     ds.setTypeface(Typeface.DEFAULT_BOLD); 
     ds.setUnderlineText(false); // set to false to remove underline 
    } 

    @Override 
    public void onClick(View widget) { 
     TextView tv = (TextView) widget; 
     Spanned s = (Spanned) tv.getText(); 
     int start = s.getSpanStart(this); 
     int end = s.getSpanEnd(this); 
     String theWord = s.subSequence(start + 1, end).toString(); 
     Toast.makeText(context, String.format("Here's a cool user: %s", theWord), 10).show(); 
     startActivity(new Intent(context, MyProfile.class)); 

    } 

這裏打開新的激活Google是我的完整代碼

public class FeedHomeAdapter extends BaseAdapter { 
private Context mContext; 
private ArrayList<String> id; 
private ArrayList<String> puName; 
private ArrayList<String> pfName; 

private SQLiteDatabase dataBaseall; 



public FeedHomeAdapter(Context c, ArrayList<String> id, ArrayList<String> uname, 

         ArrayList<String> sid, ArrayList<String> svideo, ArrayList<String> isfollow, ArrayList<String> ischat) { 
    this.mContext = c; 

    this.id = id; 
    this.puName = uname; 

    this.isfollowing = isfollow; 
    this.pischat = ischat; 

} 

public int getCount() { 
    // TODO Auto-generated method stub 
    return id.size(); 
} 

public Object getItem(int position) { 
    // TODO Auto-generated method stub 
    return null; 
} 

public long getItemId(int position) { 

    // TODO Auto-generated method stub 
    return position; 
} 

public ArrayList<int[]> getSpans(String body, char prefix) { 
    ArrayList<int[]> spans = new ArrayList<int[]>(); 

    Pattern pattern = Pattern.compile(prefix + "\\w+"); 
    Matcher matcher = pattern.matcher(body); 

    // Check all occurrences 
    while (matcher.find()) { 
     int[] currentSpan = new int[2]; 
     currentSpan[0] = matcher.start(); 
     currentSpan[1] = matcher.end(); 
     spans.add(currentSpan); 
    } 

    return spans; 
} 

public View getView(final int pos, View child, ViewGroup parent) { 
    final Holder mHolder; 
    if (child == null) { 
     child = LayoutInflater.from(mContext).inflate(R.layout.chat_display_item, parent, false); 
     LayoutInflater layoutInflater; 
     layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     child = layoutInflater.inflate(R.layout.card_layout, null); 

     mHolder = new Holder(); 
     mHolder.txt_uName = (TextView) child.findViewById(R.id.name); 
     mHolder.txt_uName1 = (TextView) child.findViewById(R.id.name1); 
     mHolder.txt_fName = (TextView) child.findViewById(R.id.fname); 
     mHolder.txt_satus = (TextView) child.findViewById(R.id.note); 


     mHolder.comment = (ImageButton) child.findViewById(R.id.replide); 

     child.setTag(mHolder); 

    } else { 
     mHolder = (Holder) child.getTag(); 
    } 
    mHolder.txt_satus.setHighlightColor(Color.WHITE); 
    mHolder.txt_satus.setHighlightColor(Color.TRANSPARENT); 



    ArrayList<int[]> hashtagSpans = getSpans(psatus.get(pos), '#'); 
    ArrayList<int[]> calloutSpans = getSpans(psatus.get(pos), '@'); 

    SpannableString commentsContent = 
      new SpannableString(this.psatus.get(pos)); 

    for (int i = 0; i < hashtagSpans.size(); i++) { 
     int[] span = hashtagSpans.get(i); 
     int hashTagStart = span[0]; 
     int hashTagEnd = span[1]; 

     commentsContent.setSpan(new Hashtag(mContext), 
       hashTagStart, 
       hashTagEnd, 0); 

    } 
    for (int i = 0; i < calloutSpans.size(); i++) { 
     int[] span = calloutSpans.get(i); 
     int calloutStart = span[0]; 
     int calloutEnd = span[1]; 

     commentsContent.setSpan(new CalloutLinkProfile(mContext), 
       calloutStart, 
       calloutEnd, 0); 

    } 



    mHolder.txt_satus.setMovementMethod(LinkMovementMethod.getInstance()); 
    mHolder.txt_satus.setText(commentsContent); 

    mHolder.txt_linktext.setText(statuslinktext.get(pos)); 
    mHolder.txt_time.setText(ptime.get(pos)); 








    return child; 
} 


public class Holder { 
    TextView txt_id; 
    TextView txt_satus; 
    TextView txt_time; 
    TextView txt_uName; 
    TextView txt_uName1; 

    LinearLayout error_d; 
    RelativeLayout profile_view; 


} 


// to check if app is install 
private boolean appInstalledOrNot(String uri) { 
    PackageManager pm = mContext.getPackageManager(); 
    boolean app_installed = false; 
    try { 
     pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES); 
     app_installed = true; 
    } catch (PackageManager.NameNotFoundException e) { 
     app_installed = false; 
    } 
    return app_installed; 
} 







class CalloutLinkProfile extends ClickableSpan { 

    Context context; 
    TextPaint textPaint; 

    public CalloutLinkProfile(Context ctx) { 
     super(); 
     context = ctx; 

    } 


    @Override 
    public void updateDrawState(TextPaint ds) { 
     textPaint = ds; 
     // ds.setColor(0xff336699); 

     ds.setARGB(255, 30, 144, 255); 
     ds.setTypeface(Typeface.DEFAULT_BOLD); 
     ds.setUnderlineText(false); // set to false to remove underline 
    } 

    @Override 
    public void onClick(View widget) { 
     TextView tv = (TextView) widget; 
     Spanned s = (Spanned) tv.getText(); 
     int start = s.getSpanStart(this); 
     int end = s.getSpanEnd(this); 
     String theWord = s.subSequence(start + 1, end).toString(); 
     Toast.makeText(context, String.format("Here's a cool user: %s", theWord), 10).show(); 
     startActivity(new Intent(context, MyProfile.class)); 

    } 



} 

}

+0

有什麼問題? – Blackbelt

+0

單擊時無法啓動新的startActivity。 – kengodmans

+0

你是什麼意思? onClick沒有被調用?你在哪裏設置了clickablespan – Blackbelt

回答

0

點擊的TextView不工作?