2011-12-27 19 views
90

Sto creando un ClickableSpan, e viene visualizzato correttamente con il testo corretto sottolineato. Tuttavia i clic non si registrano. Sai cosa sto facendo male ??? Grazie, Victor Ecco il frammento di codice:Android ClickableSpan non chiama onClick

view.setText("This is a test");  
ClickableSpan span = new ClickableSpan() { 
    @Override 
    public void onClick(View widget) { 
     log("Clicked"); 
    } 
}; 
view.getText().setSpan(span, 0, view.getText().length(), 
         Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 

risposta

258

Hai provato impostando la MovementMethod sul TextView che contiene l'arco? Hai bisogno di fare che per fare il lavoro, facendo clic ...

tv.setMovementMethod(LinkMovementMethod.getInstance()); 
+0

Non funziona bene se 'tv' è di tipo EditText, vero è possibile fare clic sul arco ma non modificare questo come normale. –

+0

grazie mille! È lavoro anche per me! puoi spiegarmi, quindi su questa impostazione? –

+0

Grazie, ha funzionato. – Prashant

1

Dopo alcuni tentativi ed errori, la sequenza di impostazione della questione tv.setMovementMethod(LinkMovementMethod.getInstance()); fa.

Ecco il mio codice completo

String stringTerms = getString(R.string.sign_up_terms); 
Spannable spannable = new SpannableString(stringTerms); 
int indexTermsStart = stringTerms.indexOf("Terms"); 
int indexTermsEnd = indexTermsStart + 18; 
spannable.setSpan(new UnderlineSpan(), indexTermsStart, indexTermsEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 
spannable.setSpan(new ForegroundColorSpan(getColor(R.color.theme)), indexTermsStart, indexTermsEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 
spannable.setSpan(new ClickableSpan() { 
    @Override 
    public void onClick(View widget) { 
     Log.d(TAG, "TODO onClick.. Terms and Condition"); 
    } 
}, indexTermsStart, indexTermsEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 

int indexPolicyStart = stringTerms.indexOf("Privacy"); 
int indexPolicyEnd = indexPolicyStart + 14; 
spannable.setSpan(new UnderlineSpan(), indexPolicyStart, indexPolicyEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 
spannable.setSpan(new ForegroundColorSpan(getColor(R.color.theme)), indexPolicyStart, indexPolicyEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 
spannable.setSpan(new ClickableSpan() { 
    @Override 
    public void onClick(View widget) { 
     Log.d(TAG, "TODO onClick.. Privacy Policy"); 
    } 
}, indexPolicyStart, indexPolicyEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 

TextView textViewTerms = (TextView) findViewById(R.id.sign_up_terms_text); 
textViewTerms.setText(spannable); 
textViewTerms.setClickable(true); 
textViewTerms.setMovementMethod(LinkMovementMethod.getInstance()); 
Problemi correlati