Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/214.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在Xamarin Android的TextView中显示源代码?_C#_Android_Xamarin_Xamarin.android - Fatal编程技术网

C# 如何在Xamarin Android的TextView中显示源代码?

C# 如何在Xamarin Android的TextView中显示源代码?,c#,android,xamarin,xamarin.android,C#,Android,Xamarin,Xamarin.android,我想开发关于编程的教育应用程序,但我不知道如何在TextView中显示源代码(Java/XML/C#/…)。 我认为这是行不通的,因为语法会混乱 myTextView.SetText("MY_SOURCE_CODE"); 那么,有人对这个问题有什么想法吗?我知道我可以像在Android Studio中一样使用库,但我不知道如何在Xamarin.Android中这样做 我想创造这样的东西 您可以参考这一点,CodeView的类型是WebView,但它可以实现相同的功能。您可以通过从nuget软件

我想开发关于编程的教育应用程序,但我不知道如何在TextView中显示源代码(Java/XML/C#/…)。 我认为这是行不通的,因为语法会混乱

myTextView.SetText("MY_SOURCE_CODE");
那么,有人对这个问题有什么想法吗?我知道我可以像在Android Studio中一样使用库,但我不知道如何在Xamarin.Android中这样做

我想创造这样的东西

您可以参考这一点,
CodeView
的类型是
WebView
,但它可以实现相同的功能。您可以通过从nuget软件包下载直接使用它:

将视图添加到布局中:

<br.tiagohm.codeview.CodeView
    android:id="@+id/codeView"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    app:cv_font_size="14"
    app:cv_highlight_line_number="36"
    app:cv_show_line_number="true"
    app:cv_start_line_number="0"
    app:cv_wrap_line="true"
    app:cv_zoom_enable="true">
</br.tiagohm.codeview.CodeView>  

使用时:

public class CodeViewActivity : AppCompatActivity, CodeView.IOnHighlightListener
{

    CodeView mCodeView;
    private ProgressDialog mProgressDialog;
    private int themePos = 0;

    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        SetContentView(Resource.Layout.codeView);
        mCodeView = FindViewById<CodeView>(Resource.Id.codeView);

        mCodeView.SetOnHighlightListener(this)
                .SetOnHighlightListener(this)
                .SetTheme(Br.Tiagohm.Theme.ArduinoLight)
                .SetCode(JAVA_CODE)
                .SetLanguage(Language.Auto)
                .SetWrapLine(true)
                .SetFontSize(14)
                .SetZoomEnabled(true)
                .SetShowLineNumber(true)
                .SetStartLineNumber(1)
                .Apply();

        mCodeView.SetTheme(Br.Tiagohm.Theme.All.ElementAt(1)).Apply();
    }

    private void setHighlightTheme(int pos)
    {
        mCodeView.SetTheme(Br.Tiagohm.Theme.All.ElementAt(pos)).Apply();
        Toast.MakeText(this, Br.Tiagohm.Theme.All.ElementAt(pos).Name, ToastLength.Short).Show();
    }

    public void OnFinishCodeHighlight()
    {
        if (mProgressDialog != null)
        {
            mProgressDialog.Dismiss();
        }

        Toast.MakeText(this, "line count: " + mCodeView.LineCount, ToastLength.Short).Show();
    }

    public void OnFontSizeChanged(int sizeInPx)
    {
        Log.Debug("TAG", "font-size: " + sizeInPx + "px");
    }

    public void OnLanguageDetected(Language language, int relevance)
    {
        Toast.MakeText(this, "language: " + language + " relevance: " + relevance, ToastLength.Short).Show();
    }

    public void OnLineClicked(int lineNumber, string content)
    {
        Toast.MakeText(this, "line: " + lineNumber + " html: " + content, ToastLength.Short).Show();
    }

    public void OnStartCodeHighlight()
    {
        mProgressDialog = ProgressDialog.Show(this, null, "Carregando...", true);
    }

    #region Code
    private static String JAVA_CODE = "package com.example.android.bluetoothchat;\n" +

       "\n" +

       "import android.os.Bundle;\n" +

       "import android.support.v4.app.FragmentTransaction;\n" +

       "import android.view.Menu;\n" +

       "import android.view.MenuItem;\n" +

       "import android.widget.ViewAnimator;\n" +

       "\n" +

       "import com.example.android.common.activities.SampleActivityBase;\n" +

       "import com.example.android.common.logger.Log;\n" +

       "import com.example.android.common.logger.LogFragment;\n" +

       "import com.example.android.common.logger.LogWrapper;\n" +

       "import com.example.android.common.logger.MessageOnlyLogFilter;\n" +

       "\n" +

       "/**\n" +

       " * A simple launcher activity containing a summary sample description, sample log and a custom\n" +

       " * {@link android.support.v4.app.Fragment} which can display a view.\n" +

       " * <p>\n" +

       " * For devices with displays with a width of 720dp or greater, the sample log is always visible,\n" +

       " * on other devices it's visibility is controlled by an item on the Action Bar.\n" +

       " */\n" +

       "public class MainActivity extends SampleActivityBase {\n" +

       "\n" +

       "    public static final String TAG = \"MainActivity\";\n" +

       "\n" +

       "    // Whether the Log Fragment is currently shown\n" +

       "    private boolean mLogShown;\n" +

       "\n" +

       "    @Override\n" +

       "    protected void onCreate(Bundle savedInstanceState) {\n" +

       "        super.onCreate(savedInstanceState);\n" +

       "        setContentView(R.layout.activity_main);\n" +

       "\n" +

       "        if (savedInstanceState == null) {\n" +

       "            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();\n" +

       "            BluetoothChatFragment fragment = new BluetoothChatFragment();\n" +

       "            transaction.replace(R.id.sample_content_fragment, fragment);\n" +

       "            transaction.commit();\n" +

       "        }\n" +

       "    }\n" +

       "\n" +

       "    @Override\n" +

       "    public boolean onCreateOptionsMenu(Menu menu) {\n" +

       "        getMenuInflater().inflate(R.menu.main, menu);\n" +

       "        return true;\n" +

       "    }\n" +

       "\n" +

       "    @Override\n" +

       "    public boolean onPrepareOptionsMenu(Menu menu) {\n" +

       "        MenuItem logToggle = menu.findItem(R.id.menu_toggle_log);\n" +

       "        logToggle.setVisible(findViewById(R.id.sample_output) instanceof ViewAnimator);\n" +

       "        logToggle.setTitle(mLogShown ? R.string.sample_hide_log : R.string.sample_show_log);\n" +

       "\n" +

       "        return super.onPrepareOptionsMenu(menu);\n" +

       "    }\n" +

       "\n" +

       "    @Override\n" +

       "    public boolean onPrepareOptionsMenu(Menu menu) {\n" +

       "        MenuItem logToggle = menu.findItem(R.id.menu_toggle_log);\n" +

       "        logToggle.setVisible(findViewById(R.id.sample_output) instanceof ViewAnimator);\n" +

       "        logToggle.setTitle(mLogShown ? R.string.sample_hide_log : R.string.sample_show_log);\n" +

       "\n" +

       "        return super.onPrepareOptionsMenu(menu);\n" +

       "    }\n" +

       "\n" +

       "    @Override\n" +

       "    public boolean onPrepareOptionsMenu(Menu menu) {\n" +

       "        MenuItem logToggle = menu.findItem(R.id.menu_toggle_log);\n" +

       "        logToggle.setVisible(findViewById(R.id.sample_output) instanceof ViewAnimator);\n" +

       "        logToggle.setTitle(mLogShown ? R.string.sample_hide_log : R.string.sample_show_log);\n" +

       "\n" +

       "        return super.onPrepareOptionsMenu(menu);\n" +

       "    }\n" +

       "\n" +

       "    @Override\n" +

       "    public boolean onOptionsItemSelected(MenuItem item) {\n" +

       "        switch(item.getItemId()) {\n" +

       "            case R.id.menu_toggle_log:\n" +

       "                mLogShown = !mLogShown;\n" +

       "                ViewAnimator output = (ViewAnimator) findViewById(R.id.sample_output);\n" +

       "                if (mLogShown) {\n" +

       "                    output.setDisplayedChild(1);\n" +

       "                } else {\n" +

       "                    output.setDisplayedChild(0);\n" +

       "                }\n" +

       "                supportInvalidateOptionsMenu();\n" +

       "                return true;\n" +

       "        }\n" +

       "        return super.onOptionsItemSelected(item);\n" +

       "    }\n" +

       "\n" +

       "    /** Create a chain of targets that will receive log data */\n" +

       "    @Override\n" +

       "    public void initializeLogging() {\n" +

       "        // Wraps Android's native log framework.\n" +

       "        LogWrapper logWrapper = new LogWrapper();\n" +

       "        // Using Log, front-end to the logging chain, emulates android.util.log method signatures.\n" +

       "        Log.setLogNode(logWrapper);\n" +

       "\n" +

       "        // Filter strips out everything except the message text.\n" +

       "        MessageOnlyLogFilter msgFilter = new MessageOnlyLogFilter();\n" +

       "        logWrapper.setNext(msgFilter);\n" +

       "\n" +

       "        // On screen logging via a fragment with a TextView.\n" +

       "        LogFragment logFragment = (LogFragment) getSupportFragmentManager()\n" +

       "                .findFragmentById(R.id.log_fragment);\n" +

       "        msgFilter.setNext(logFragment.getLogView());\n" +

       "\n" +

       "        Log.i(TAG, \"Ready\");\n" +

       "    }\n" +

       "}";
    #endregion
}
公共类CodeViewActivity:AppCompatActivity,CodeView.IOnHighlightListener
{
CodeView-mCodeView;
private ProgressDialog mProgressDialog;
私有int themePos=0;
创建时受保护的覆盖无效(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.codeView);
mCodeView=findviewbyd(Resource.Id.codeView);
mCodeView.SetOnHighlightListener(此)
.SetOnHighlightListener(此)
.SetTheme(Br.Tiagohm.Theme.ArduinoLight)
.SetCode(JAVA_代码)
.SetLanguage(Language.Auto)
.SetWrapLine(真)
.SetFontSize(14)
.SetZoomEnabled(真)
.SetShowLineNumber(真)
.SetStartLineNumber(1)
.Apply();
SetTheme(Br.Tiagohm.Theme.All.ElementAt(1)).Apply();
}
私有void setHighlightTheme(int-pos)
{
SetTheme(Br.Tiagohm.Theme.All.ElementAt(pos)).Apply();
Toast.MakeText(this,Br.Tiagohm.Theme.All.ElementAt(pos.Name,ToastLength.Short).Show();
}
公共无效OnFinishCodeHighlight()
{
如果(mProgressDialog!=null)
{
mProgressDialog.disclose();
}
Toast.MakeText(此“行计数:+mCodeView.LineCount,ToastLength.Short).Show();
}
公共无效OnFontSizeChanged(int sizeInPx)
{
Log.Debug(“标记”,“字体大小:”+sizeInPx+“px”);
}
仅检测到public void Language(语言、int相关性)
{
Toast.MakeText(这个,“语言:+language+”关联:+relevance,ToastLength.Short).Show();
}
联机单击公共void(整数行号,字符串内容)
{
Toast.MakeText(此,“行:”+lineNumber+“html:”+content,ToastLength.Short).Show();
}
public void OnStartCodeHighlight()
{
mProgressDialog=ProgressDialog.Show(这个,null,“Carregando…”,true);
}
#区号
私有静态字符串JAVA\u CODE=“package com.example.android.bluetoothchat;\n”+
“\n”+
“导入android.os.Bundle;\n”+
“导入android.support.v4.app.FragmentTransaction;\n”+
“导入android.view.Menu;\n”+
“导入android.view.MenuItem;\n”+
“导入android.widget.ViewAnimator;\n”+
“\n”+
“导入com.example.android.common.activities.SampleActivityBase;\n”+
“导入com.example.android.common.logger.Log;\n”+
“导入com.example.android.common.logger.LogFragment;\n”+
“导入com.example.android.common.logger.LogWrapper;\n”+
“导入com.example.android.common.logger.MessageOnlyLogFilter;\n”+
“\n”+
“/***\n”+
*一个简单的启动程序活动,包含摘要示例说明、示例日志和自定义\n+
“*{@link android.support.v4.app.Fragment}可以显示视图。\n”+
“*\n”+
*对于显示器宽度为720dp或更大的设备,示例日志始终可见,\n+
“*在其他设备上,其可见性由操作栏上的项目控制。\n”+
“*/\n”+
“公共类MainActivity扩展SampleActivityBase{\n”+
“\n”+
“公共静态最终字符串标记=\“MainActivity\”;\n”+
“\n”+
“//当前是否显示日志片段\n”+
“已显示专用布尔值MLOG;\n”+
“\n”+
“@覆盖\n”+
“创建时受保护的void(Bundle savedInstanceState){\n”+
“super.onCreate(savedInstanceState);\n”+
“setContentView(R.layout.activity_main);\n”+
“\n”+
“如果(savedInstanceState==null){\n”+
“FragmentTransaction=getSupportFragmentManager().beginTransaction();\n”+
“BluetoothChatFragment=新的BluetoothChatFragment();\n”+
事务。替换(R.id.sample\u content\u fragment,fragment);\n+
“事务。提交();\n”+
“}\n”+
“}\n”+
“\n”+
“@覆盖\n”+
“公共布尔onCreateOptions菜单(菜单菜单){\n”+
getMenuInflater().充气(R.menu.main,menu);\n+
“返回true;\n”+
“}\n”+
“\n”+
“@覆盖\n”+
“公共布尔值OnPrepareOptions菜单(菜单){\n”+
“MenuItem logToggle=menu.findItem(R.id.menu\u toggle\u log);\n”+
“logToggle.setVisible(findViewById(R.id.sample_输出)ViewAnimator实例);\n”+
“logToggle.setTitle(mlogshow?R.string.sample_hide_log:R.string.sample_show_log);\n”+
“\n”+
“返回super.onPrepareOptions菜单(菜单);\n”+
“}\n”+
“\n”+
“@覆盖\n”+
“公共布尔值OnPrepareOptions菜单(菜单){\n”+
“MenuItem logToggle=menu.findItem(R.id.menu\u toggle\u log);\n”+
“logToggle.setVisible(findViewById(R.id.sample_输出)ViewAnimator实例);\n”+
“logToggle.setTitle(mlogshow?R.string.sample_hide_log:R.string.sample_show_log);\n”+
“\n”+