2017-02-06 5 views
-2

Ich arbeite an einem Android-Projekt mit einigen TextViews für Studenten, aber ich möchte, dass Benutzer lange klicken und einige Teile des Inhalts mit verschiedenen Farben markieren, wie es in einigen passiert Bibel-Apps oder Adobe Reader. Die Option zum Markieren von Textfarbe geschieht bei langem Klick. Ich freue mich über jede Anleitung, damit dies funktioniert.Wie man TextView hervorheben kann

+0

Möchten Sie das? https://android--code.blogspot.in/2016/03/android-highlight-text-in-textview.html –

+0

Ihre Frage ist unklar, beziehen Sie sich auf diese: http://stackoverflow.com/help/how- zu fragen –

+0

Ich habe die Frage aktualisiert. Vielen Dank. – user2810726

Antwort

0

Verwendung Spannable Zeichenfolge, die den Test Beispiel stylen:

String firstWord = "Hello"; 
String secondWord = "World!"; 

TextView tvHelloWorld = (TextView)findViewById(R.id.tvHelloWorld); 

// Create a span that will make the text red 
ForegroundColorSpan redForegroundColorSpan = new ForegroundColorSpan(
     getResources().getColor(android.R.color.holo_red_dark)); 

// Use a SpannableStringBuilder so that both the text and the spans are mutable 
SpannableStringBuilder ssb = new SpannableStringBuilder(firstWord); 

// Apply the color span 
ssb.setSpan(
     redForegroundColorSpan,   // the span to add 
     0,         // the start of the span (inclusive) 
     ssb.length(),      // the end of the span (exclusive) 
     Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); // behavior when text is later inserted into the SpannableStringBuilder 
              // SPAN_EXCLUSIVE_EXCLUSIVE means to not extend the span when additional 
              // text is added in later 

// Add a blank space 
ssb.append(" "); 

// Create a span that will strikethrough the text 
StrikethroughSpan strikethroughSpan = new StrikethroughSpan(); 

// Add the secondWord and apply the strikethrough span to only the second word 
ssb.append(secondWord); 
ssb.setSpan(
     strikethroughSpan, 
     ssb.length() - secondWord.length(), 
     ssb.length(), 
     Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 

// Set the TextView text and denote that it is Editable 
// since it's a SpannableStringBuilder 
tvHelloWorld.setText(ssb, TextView.BufferType.EDITABLE); 

enter image description here

Quelle: https://guides.codepath.com/android/Working-with-the-TextView

+0

Sorry das ist nicht was ich wollte. Ich habe die Frage aktualisiert, um zu reflektieren, was ich wirklich brauche. Ich möchte, dass Benutzer lange auf die Textansicht klicken, um die Option zu erhalten, einige Teile der Texte zu markieren. – user2810726

+0

entsprechend der Aktualisierung Ihres Posts: Sie müssen in jedem Wort clickablespan setzen, wenn eine Arbeit geklickt wird, dann ändern Sie die Schriftgröße oder Farbe Starten Sie die Animation, die ausgewählt ist. clickablespan und SpannableString nur Unterstützung klicken es nicht unterstützt longclick –

0

Besser für eine Bibliothek zu verwenden. Siehe diesen Link. https://github.com/athkalia/EmphasisTextView

+0

lassen Sie mich wissen, es ist wertvoll oder nicht ... – Champandorid

+0

Sorry, das ist nicht das, was ich wollte. Ich habe die Frage aktualisiert, um zu reflektieren, was ich wirklich brauche. Ich möchte, dass Benutzer lange auf die Textansicht klicken, um die Option zu erhalten, einige Teile der Texte hervorzuheben – user2810726

Verwandte Themen