2017-11-17 15 views
0

プログラミングに関する教育アプリを開発したいのですが、TextViewにソースコード(Java/XML/C#/ ...)を表示する方法をkhowではありません。 構文がうまくいかないので、これは動作しないと思います。Xamarin AndroidのTextViewにソースコードを表示するには?

だから誰もこの問題のアイデアはありますか?私はAndroid StudioでCodeViewのようなライブラリを使うことができると知っていますが、Xamarin.Androidでこれを行う方法はわかりません。

私はこのようなものを作りたいと思います。

image2

+0

このJavaライブラリ用のバインディングライブラリを作成できます。https://developer.xamarin.com/guides/android/advanced_topics/binding-a-java-library/ – SushiHangover

答えて

0

あなたはこのCodeView libraryを参照してください可能性があり、CodeViewのタイプはWebViewですが、それは、同じ機能を実装することができます。そして、あなたは直接nugetパッケージからダウンロードして、それを使用してできます。

enter image description here

は、あなたのレイアウトにビューを追加します。

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

Effect

関連する問題