Analyze some associations between native and html
1. Set the color change of some fonts in Android and be able to click
1, use SpannableStringBuilder to implement
//1,使用 SpannableStringBuilder , 参数中的数字表示修改的片段的起始位置和结束位置 TextView tv_1 = (TextView) findViewById(R.id.textView_1); String str_1 = "使用 SpannableStringBuilder 来实现部分字体颜色的改变"; SpannableStringBuilder ssb = new SpannableStringBuilder(str_1); ssb.setSpan(new ForegroundColorSpan(Color.RED), 0, 10,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE ); ssb.setSpan(new ForegroundColorSpan(Color.YELLOW), 12, 22,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE ); ssb.setSpan(new ForegroundColorSpan(Color.GREEN), 23, str_1.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE ); tv_1.setText(ssb);
2, use html to implement
//2,使用html来修改部分字体的颜色 TextView tv_2 = (TextView) findViewById(R.id.textView_2); String str_2 = "使用 Html 来实现部分字体颜色的改变"; tv_2.setText(Html.fromHtml("使用 Html <font color = blue> 来实现部分字体颜色的改变</font>"));
html = "<html><body>" + "<p><font color=\"#FFBF00\"> ② </p>" + "<p><font color=\"#CE00F7\">城郊 " + "</p>" + "</body></html>"; _Holder.station_change.setText(Html.fromHtml(html));
3 , use SpannableStringBuilder to implement, or SpannableString to change the color of some fonts and be clickable. ClickableSpan
//3,实现部分字体颜色的改变,并能点击 TextView tv_3 = (TextView) findViewById(R.id.textView_3); String str_3 = "实现部分字体颜"; String str_4 = "色的改变并且能点击"; //这里无论是使用 SpannableString 还是 SpannableStringBuilder 都一样 SpannableString ss = new SpannableString(str_4); // SpannableStringBuilder s = new SpannableStringBuilder(str_4); MyClickableSpan clickSpan = new MyClickableSpan(this, str_4); ss.setSpan(clickSpan, 0, str_4.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); tv_3.setText(str_3); tv_3.append(ss); //必须加这一句,否则就无法被点击 tv_3.setMovementMethod(LinkMovementMethod.getInstance());
/** * 这个类 实际上和第一种改变颜色的方法差不多,只不过 那是个专门改变颜色的Span,这是个专门负责点击处理的Span * @author Administrator */ class MyClickableSpan extends ClickableSpan{ private Context context; private String text; public MyClickableSpan(Context context,String text) { this.context = context; this.text = text; } //在这里设置字体的大小,等待各种属性 public void updateDrawState(TextPaint ds) { ds.setColor(Color.RED); } @Override public void onClick(View widget) { Intent intent = new Intent(MainActivity.this,OtherActivity.class); startActivity(intent); } }
2. Interaction between Android native code and HTML5
1. Native The code calls the HTML5 page method
For example, the app needs to call the changeColor(color) method of the HTML5 page to change the color of the HTML5 page
1) HTML5
<script type="text/javascript"> document.write("Hello World!") function changeColor(color){ document.body.style.background = color; } </script>
2) Android
//开启JavaScript支持 wvMain.getSettings().setJavaScriptEnabled(true); //放在assets的html需加上android_asset/ ;也可以用网络上的文件 wvMain.loadUrl("file:///android_asset/show.html"); // 添加一个对象, 让JS可以访问该对象的方法, 该对象中可以调用JS中的方法 wvMain.addJavascriptInterface(new JSInterface1(),"baobao"); btnOne.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String color = "#cccccc"; wvMain.loadUrl("javascript: changeColor('"+color+"')"); }});
2.HTLM5 page calls native method
For example, click on the text of the HTML5 page and call back the callAndroidMethod method in the native code
1) HTML5
<a onClick="baobao.callAndroidMethod(100,100,'ccc',true)">CallAndroidMethod</a>
2 )android
class JSInterface1 { //JavaScript调用此方法 @JavascriptInterface public void callAndroidMethod(int a,float b, String c,boolean d){ if(d){ String strMessage = "a+b+c="+a+b+c; new AlertDialog.Builder(MainActivity.this).setTitle("title").setMessage(strMessage).show(); } } }
1. Set the color change of some fonts in Android, and click
1, use SpannableStringBuilder to achieve
//1,使用 SpannableStringBuilder , 参数中的数字表示修改的片段的起始位置和结束位置 TextView tv_1 = (TextView) findViewById(R.id.textView_1); String str_1 = "使用 SpannableStringBuilder 来实现部分字体颜色的改变"; SpannableStringBuilder ssb = new SpannableStringBuilder(str_1); ssb.setSpan(new ForegroundColorSpan(Color.RED), 0, 10,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE ); ssb.setSpan(new ForegroundColorSpan(Color.YELLOW), 12, 22,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE ); ssb.setSpan(new ForegroundColorSpan(Color.GREEN), 23, str_1.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE ); tv_1.setText(ssb);
2, Use html to implement
//2,使用html来修改部分字体的颜色 TextView tv_2 = (TextView) findViewById(R.id.textView_2); String str_2 = "使用 Html 来实现部分字体颜色的改变"; tv_2.setText(Html.fromHtml("使用 Html <font color = blue> 来实现部分字体颜色的改变</font>"));
或者 html = "<html><body>" + "<p><font color=\"#FFBF00\"> ② </p>" + "<p><font color=\"#CE00F7\">城郊 " + "</p>" + "</body></html>"; _Holder.station_change.setText(Html.fromHtml(html));
3, use SpannableStringBuilder to implement it, or SpannableString to implement the color change of some fonts and make it clickable. ClickableSpan
//3,实现部分字体颜色的改变,并能点击 TextView tv_3 = (TextView) findViewById(R.id.textView_3); String str_3 = "实现部分字体颜"; String str_4 = "色的改变并且能点击"; //这里无论是使用 SpannableString 还是 SpannableStringBuilder 都一样 SpannableString ss = new SpannableString(str_4); // SpannableStringBuilder s = new SpannableStringBuilder(str_4); MyClickableSpan clickSpan = new MyClickableSpan(this, str_4); ss.setSpan(clickSpan, 0, str_4.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); tv_3.setText(str_3); tv_3.append(ss); //必须加这一句,否则就无法被点击 tv_3.setMovementMethod(LinkMovementMethod.getInstance());
/** * 这个类 实际上和第一种改变颜色的方法差不多,只不过 那是个专门改变颜色的Span,这是个专门负责点击处理的Span * @author Administrator */ class MyClickableSpan extends ClickableSpan{ private Context context; private String text; public MyClickableSpan(Context context,String text) { this.context = context; this.text = text; } //在这里设置字体的大小,等待各种属性 public void updateDrawState(TextPaint ds) { ds.setColor(Color.RED); } @Override public void onClick(View widget) { Intent intent = new Intent(MainActivity.this,OtherActivity.class); startActivity(intent); } }
2 is used here. Android native code and HTML5 interaction
1. Native code calls HTML5 page methods
For example, the app needs to call the changeColor(color) method of the HTML5 page to change the color of the HTML5 page
1) HTML5
<script type="text/javascript"> document.write("Hello World!") function changeColor(color){ document.body.style.background = color; } </script>
2) Android
//开启JavaScript支持 wvMain.getSettings().setJavaScriptEnabled(true); //放在assets的html需加上android_asset/ ;也可以用网络上的文件 wvMain.loadUrl("file:///android_asset/show.html"); // 添加一个对象, 让JS可以访问该对象的方法, 该对象中可以调用JS中的方法 wvMain.addJavascriptInterface(new JSInterface1(),"baobao"); btnOne.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String color = "#cccccc"; wvMain.loadUrl("javascript: changeColor('"+color+"')"); }});
2.HTLM5 page calls native method
For example, click on the text of the HTML5 page and call back the callAndroidMethod method in the native code
1) HTML5
<a onClick="baobao.callAndroidMethod(100,100,'ccc',true)">CallAndroidMethod</a>
2 )android
class JSInterface1 { //JavaScript调用此方法 @JavascriptInterface public void callAndroidMethod(int a,float b, String c,boolean d){ if(d){ String strMessage = "a+b+c="+a+b+c; new AlertDialog.Builder(MainActivity.this).setTitle("title").setMessage(strMessage).show(); } } }
The above is the detailed content of Analyze some associations between native and html. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.
