Analyze some associations between native and html

零下一度
Release: 2017-04-27 11:55:35
Original
1600 people have browsed it

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);
Copy after login
Copy after login

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  来实现部分字体颜色的改变"));
Copy after login
Copy after login
html = "" + "

" + "

城郊 " + "

" + ""; _Holder.station_change.setText(Html.fromHtml(html));
Copy after login
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());
Copy after login
Copy after login
/** * 这个类 实际上和第一种改变颜色的方法差不多,只不过 那是个专门改变颜色的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); } }
Copy after login
Copy after login

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

Copy after login
Copy after login

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+"')"); }});
Copy after login
Copy after login

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

CallAndroidMethod
Copy after login
Copy after login

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(); } } }
Copy after login
Copy after login

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);
Copy after login
Copy after login

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  来实现部分字体颜色的改变"));
Copy after login
Copy after login
或者 html = "" + "

" + "

城郊 " + "

" + ""; _Holder.station_change.setText(Html.fromHtml(html));
Copy after login
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());
Copy after login
Copy after login
/** * 这个类 实际上和第一种改变颜色的方法差不多,只不过 那是个专门改变颜色的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); } }
Copy after login
Copy after login

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

Copy after login
Copy after login

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+"')"); }});
Copy after login
Copy after login

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

CallAndroidMethod
Copy after login
Copy after login

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(); } } }
Copy after login
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!