我想开发关于编程的教育应用程序,但我不知道如何在TextView中显示源代码(Java / XML / C#/ ...)。 我认为这不起作用,因为语法会搞砸。
myTextView.SetText("MY_SOURCE_CODE");
那么,有人对这个问题有所了解吗?我知道我可以在Android Studio中使用类似CodeView的库,但我不知道如何在Xamarin.Android中执行此操作
我想创建这样的东西。
答案 0 :(得分:0)
您可以参考此CodeView library,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
}