1.Android中設置部分字體的顏色改變,并且能點擊
1, 使用SpannableStringBuilder來實現//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, 使用 html 來實現
//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 , 使用SpannableStringBuilder來實現,或者 SpannableString來實現部分字體的顏色的改變,并且能點擊,這里用到了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.Android原生代碼與HTML5的交互1.原生代碼調用HTML5頁面方法
例如,app要調用HTML5頁面的changeColor(color)的方法,來改變HTML5頁面的顏色
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頁面調用原生方法
例如,點擊HTML5頁面的文字,回調原生代碼中的callAndroidMethod方法
1)HTML5
a quot;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.Android中設置部分字體的顏色改變,并且能點擊1, 使用SpannableStringBuilder來實現//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, 使用 html 來實現
//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 , 使用SpannableStringBuilder來實現,或者 SpannableString來實現部分字體的顏色的改變,并且能點擊,這里用到了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.Android原生代碼與HTML5的交互1.原生代碼調用HTML5頁面方法例如,app要調用HTML5頁面的changeColor(color)的方法,來改變HTML5頁面的顏色
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頁面調用原生方法
例如,點擊HTML5頁面的文字,回調原生代碼中的callAndroidMethod方法
1)HTML5
a quot;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(); }以上就是解析原生與html之間進行的一些關聯的詳細內容,html教程
鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。
新聞熱點
疑難解答