How can I build up a display using an EditText with different colors for command and response strings?

StackOverflow https://stackoverflow.com/questions/13904959

Frage

I am trying to implement a hypertext-like application so I can use a tablet via USB to talk to a PIC system. My concept is to have a fragment showing two EditText views, one to allow a command to be composed and edited on the tablet, the other to hold all the traffic in both directions.

I have got as far as getting the composed string sorted and added to the traffic view. However, I would like to be able to show the tablet-generated strings in a different color to that used for the PIC-generated replies. I have tried numerous combinations and permutations of setSpan but with no luck - any ideas? I am also open to other ways of achieving the same end.

The Java code I am using is:

package durdle.bruce.fragment_trial;

import android.app.Fragment;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnKeyListener;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

public class PanelFragment extends Fragment implements OnKeyListener
{
    private final String TAG = "Panel Fragment:- ";
    EditText cmdLine;
    EditText traffic_panel;
    int lineCount = 0;
    CharSequence cs;
    CharSequence nextLine;
    SpannableStringBuilder cmdString;
    SpannableStringBuilder traffic;
    SpannableStringBuilder ss;
    ForegroundColorSpan cmdTextColor;
    ForegroundColorSpan trafTextColor;
    String cntString;
    char newLine = '\n';

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) 
    {
        View view = inflater.inflate(R.layout.textfragment,container, false);
        cmdLine = (EditText) view.findViewById(R.id.enterCmd);
        cmdLine.setOnKeyListener(this);
        cmdTextColor = new ForegroundColorSpan(0x0000FF);
        trafTextColor = new ForegroundColorSpan(0xFF0000);
        traffic = new SpannableStringBuilder();
        return view;
    }  

    public boolean onKey(View v, int keyCode, KeyEvent event) 
    {
        if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
            (keyCode == KeyEvent.KEYCODE_ENTER))
        {
            traffic_panel = (EditText) getView().findViewById(R.id.detailsText);

            cmdString = (SpannableStringBuilder) cmdLine.getText();
            Log.d(TAG,"ENTER pressed " + cmdString);

            cs = String.valueOf(lineCount) + "-" + '\n';
            ss = new SpannableStringBuilder(cs);
            ss.setSpan(trafTextColor,0,ss.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            traffic = traffic.append(ss);

            cmdString.setSpan(cmdTextColor,0,cmdString.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            nextLine = cmdString.append(newLine);
            traffic = traffic.append(nextLine);

            Log.d(TAG, "traffic string = " + traffic);
            ((TextView) traffic_panel).setText(traffic);
            cmdLine.setText("");
            lineCount++;
        }
        return false;
    }
} 

Working code with Html.fromHtml is:

package durdle.bruce.fragment_trial;

import android.app.Fragment;
import android.os.Bundle;
import android.text.Html;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnKeyListener;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

public class PanelFragment extends Fragment implements OnKeyListener
{
private final String TAG = "Panel Fragment:- ";
EditText cmdLine;
TextView traffic_panel;
int lineCount = 0;
int oldTrafficLength;
int newTrafficLength;
CharSequence cs;
CharSequence nextLine;
SpannableStringBuilder cmdString;
SpannableStringBuilder traffic;
SpannableStringBuilder ss;
ForegroundColorSpan cmdTextColor;
ForegroundColorSpan trafTextColor;
String cntString;
char newLine = '\n';

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
  Bundle savedInstanceState) 
{
    View view = inflater.inflate(R.layout.textfragment,container, false);
    cmdLine = (EditText) view.findViewById(R.id.enterCmd);
    cmdLine.setOnKeyListener(this);
    cmdTextColor = new ForegroundColorSpan(0xFF0000);
    trafTextColor = new ForegroundColorSpan(0x0000FF);
    traffic = new SpannableStringBuilder();
    return view;
}  

public boolean onKey(View v, int keyCode, KeyEvent event) 
{
    if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
            (keyCode == KeyEvent.KEYCODE_ENTER))
    {
        traffic_panel = (TextView) getView().findViewById(R.id.detailsText);

        cmdString = (SpannableStringBuilder) cmdLine.getText();
        Log.d(TAG,"ENTER pressed " + cmdString);
        cmdString = cmdString.append(newLine);

        lineCount++;
        cs = String.valueOf(lineCount) + "-" + '\n';
        traffic = traffic.append(Html.fromHtml("<font color=\"blue\">" + cs + "</font>"));
        traffic = traffic.append('\n');

        traffic = traffic.append(Html.fromHtml("<font color=\"red\">" + cmdString + "</font>"));
        traffic.append('\n');

        ((TextView) traffic_panel).setText(traffic);
        cmdLine.setText("");
    }
    return false;
}

}

War es hilfreich?

Lösung

You can use HTML font markup and use Html.fromHtml.

just do:

TextView tv;
tv.setText(Html.fromHtml("<font color=\"red\">" + string + "</font>"));

Here is a link with the tags supported by Html.fromHtml

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top