2017-11-17 4 views
0

Ich möchte Bildungs-App über die Programmierung zu entwickeln, aber ich zeige nicht, wie Sie den Quellcode (Java/XML/C#/...) in TextView anzeigen. Ich denke, das ist nicht funktionieren, weil Syntax wird versauen.Wie wird der Quellcode in TextView in Xamarin Android angezeigt?

myTextView.SetText("MY_SOURCE_CODE"); 

Also, hat jemand eine Idee für dieses Problem? Ich weiß, ich könnte Bibliothek wie CodeView in Android Studio verwenden, aber ich habe keine Ahnung, wie man das in Xamarin.Android

Ich möchte etwas wie das erstellen.

image2

+0

Sie können eine Bindungsbibliothek für diese Java-Bibliothek erstellen: https://developer.xamarin.com/guides/android/advanced_topics/binding-a-java-library/ – SushiHangover

Antwort

0

Sie auf diese CodeView library beziehen könnte, die CodeView ‚s-Typ ist ein WebView aber es könnte die gleiche Funktion implementieren. Und man konnte mit ihm direkt per Download von nuget Paket:

enter image description here

die Ansicht auf Ihrem Layout hinzufügen:

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

Wenn es verwenden:

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 
} 

Effect.

Verwandte Themen