Home > Article > Web Front-end > Analyze some associations between native and html
1. Set the color change of some fonts in Android and be able to click
//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,实现部分字体颜色的改变,并能点击 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); } }
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,使用 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,实现部分字体颜色的改变,并能点击 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); } }
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!