Hallo zusammen,
folgende Situation: ihr wollt in ein EditText farbige Texte hinzufügen. Ihr verfügt über ein String in dem sich bestimmte Zeichen befinden die auf die Farbe hinweisen, die ihr setzen müsst. Diese Zeichen müssen am Ende entfernt werden um ein "sauberes" String auszugeben.
Wie würdet ihr also Zeichen gegen Farbe ersetzen?
Ich persönlich bekomme es mit Zeichen ohne Probleme hin.
Aber ohne Zeichen bekomme ich es nicht sauber hin. Ich verstehe nicht warum. Ich habe alles was mir eingefallen und für mich realistisch ist gemacht, um es alleine zu schaffen. Aber mein logisches Denken lässt nach...
Hier meine ganze Klasse:
Wie könnte sich das Problem lösen lassen?
LG und vielen Dank!
PD.: Ohne Zeichen habe ich zunächst nur mit der Farbe Navy und fett gedruckt ausprobieren wollen...
folgende Situation: ihr wollt in ein EditText farbige Texte hinzufügen. Ihr verfügt über ein String in dem sich bestimmte Zeichen befinden die auf die Farbe hinweisen, die ihr setzen müsst. Diese Zeichen müssen am Ende entfernt werden um ein "sauberes" String auszugeben.
Wie würdet ihr also Zeichen gegen Farbe ersetzen?
Ich persönlich bekomme es mit Zeichen ohne Probleme hin.
Aber ohne Zeichen bekomme ich es nicht sauber hin. Ich verstehe nicht warum. Ich habe alles was mir eingefallen und für mich realistisch ist gemacht, um es alleine zu schaffen. Aber mein logisches Denken lässt nach...
Hier meine ganze Klasse:
Java:
public class MainActivity extends Activity
{
EditText e;
String[] cases = {
new String(new byte[]{3,48,50}), //navy
new String(new byte[]{6}), //bold
new String(new byte[]{5,49,51}), //fushia background
new String(new byte[]{3,49,49}), //aqua
new String(new byte[]{3,48,57}), //lime
new String(new byte[]{5,48,48}), //white blackground
new String(new byte[]{5,48,52}), //red background
new String(new byte[]{3,49,50}), ////blue
new String(new byte[]{5,48,57}), //lime background
new String(new byte[]{3,48,52}), //red
new String(new byte[]{3,48,54}), //purple
new String(new byte[]{3,49,51})}; //fushia
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
e = (EditText) findViewById(R.id.chatEditText1);
byte[] b = new byte[]{5, 48, 48, 3, 48, 50, 32, 3, 48, 50, 84, 101, 109, 112, 108, 97, 116, 101, 32, 101, 114, 115, 116, 101, 108, 108, 116, 32, 118, 111, 110, 32, 32, 32, 6, 5, 49, 51, 3, 49, 49, 91,
3, 48, 57,
-30, -103, -93, 3, 49, 49,
93,
5, 48, 48,
32,
5, 48, 52,
3, 49, 50, 91, 3, 49, 49, -30, -103, -93, 3, 49, 50, 93,
5, 48, 48, 32,
5, 48, 57,
3, 48, 52, 91,
3, 48, 54, -30, -103, -93, 3, 48, 52, 93, 5, 48, 48, 32,
3, 49, 51,
116, -32, -72, -83, 109, 109, 121, 32, 91, -30, -103, -93, 93, 32, 5, 48, 57, 3, 48, 52, 91, 3, 48, 54, -30, -103, -93, 3, 48, 52, 93,
5, 48, 48, 32, 5, 48, 52, 3, 49, 50, 91, 3, 49, 49, -30, -103, -93, 3, 49, 50, 93, 5, 48, 48, 32, 5, 49, 51, 3, 49, 49, 91, 3, 48, 57, -30, -103, -93, 3, 49, 49, 93};
String chatText = new String(b);
Spannable modifiedText = new SpannableString(chatText);
for(int f=0;f<chatText.length();f++){
String ls = chatText.substring(f,chatText.length());
int i;
for(i = 0; i < cases.length; i++)
if(ls.startsWith(cases[i])) break;
switch(i) {
case 0:
modifiedText.setSpan(new ForegroundColorSpan(Color.parseColor("#000080")), f, chatText.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
break;
case 1:
modifiedText.setSpan(new StyleSpan(Typeface.BOLD),f,chatText.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
break;
case 2:
modifiedText.setSpan(new BackgroundColorSpan(Color.parseColor("#ff00ff")),f,chatText.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
break;
case 3:
modifiedText.setSpan(new ForegroundColorSpan(Color.parseColor("#00FFFF")), f, chatText.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
break;
case 4:
modifiedText.setSpan(new ForegroundColorSpan(Color.parseColor("#00FF00")), f, chatText.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
break;
case 5:
modifiedText.setSpan(new BackgroundColorSpan(Color.parseColor("#FFFFFF")), f, chatText.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
break;
case 6:
modifiedText.setSpan(new BackgroundColorSpan(Color.parseColor("#FF0000")), f, chatText.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
break;
case 7:
modifiedText.setSpan(new ForegroundColorSpan(Color.BLUE), f, chatText.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
break;
case 8:
modifiedText.setSpan(new BackgroundColorSpan(Color.parseColor("#00FF00")), f, chatText.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
break;
case 9:
modifiedText.setSpan(new ForegroundColorSpan(Color.parseColor("#FF0000")), f, chatText.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
break;
case 10:
modifiedText.setSpan(new ForegroundColorSpan(Color.parseColor("#990099")), f, chatText.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
break;
case 11:
modifiedText.setSpan(new ForegroundColorSpan(Color.parseColor("#ff00ff")), f, chatText.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
break;
}
}
e.append(modifiedText); //Mit Zeichen
String chatText2 = new String(b);
String cleanText = clean(chatText2);
Spannable modifiedText2 = new SpannableString(cleanText);
int[] i = new int[]{};
Object[] o = new Object[]{};
for(int f=0; f<chatText2.length(); f++){
String lc = chatText2.substring(f);
if(lc.startsWith(new String(new byte[]{3,48,50}))){
i = Arrays.copyOf(i,i.length + 1);
i[i.length-1] = f;
o = Arrays.copyOf(o,o.length + 1);
o[o.length-1] = new ForegroundColorSpan(Color.parseColor("#000080"));
chatText2 = chatText2.replaceFirst(new String(new byte[]{3,48,50}),"");
}
else if(lc.startsWith(new String(new byte[]{6}))){
i = Arrays.copyOf(i,i.length + 1);
i[i.length-1] = f;
o = Arrays.copyOf(o,o.length + 1);
o[o.length-1] = new StyleSpan(Typeface.BOLD);
chatText2 = chatText2.replaceFirst(new String(new byte[]{6}),"");
}
}
for(int f=0; f<i.length; f++){
modifiedText2.setSpan(o[f],i[f],cleanText.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
e.append("\n");
e.append(modifiedText2); //Ohne Zeichen
}
public String clean(String s){
for(int i = 0; i < cases.length; i++){
s= s.replaceAll(cases[i],"");
}
return s;
}
}
Wie könnte sich das Problem lösen lassen?
LG und vielen Dank!
PD.: Ohne Zeichen habe ich zunächst nur mit der Farbe Navy und fett gedruckt ausprobieren wollen...
Zuletzt bearbeitet: