0

我正在尝试实现一个类似超文本的应用程序,这样我就可以通过 USB 使用平板电脑与 PIC 系统对话。我的概念是有一个片段显示两个 EditText 视图,一个允许在平板电脑上编写和编辑命令,另一个用于保持两个方向的所有流量。

我已经将组合字符串排序并添加到流量视图中。但是,我希望能够以与 PIC 生成的回复不同的颜色显示平板电脑生成的字符串。我尝试了许多 setSpan 的组合和排列,但没有运气 - 有什么想法吗?我也对达到同样目的的其他方式持开放态度。

我正在使用的 Java 代码是:

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;
    }
} 

使用 Html.fromHtml 的工作代码是:

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;
}

}

4

1 回答 1

0

您可以使用 HTMLfont标记并使用Html.fromHtml

做就是了:

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

这是一个带有支持的标签的链接Html.fromHtml

于 2012-12-16T20:08:50.977 回答