Home  >  Article  >  Web Front-end  >  Analyze some associations between native and html

Analyze some associations between native and html

零下一度
零下一度Original
2017-04-27 11:55:351683browse

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(&#39;"+color+"&#39;)");    }});

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,&#39;ccc&#39;,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(&#39;"+color+"&#39;)");    }});

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,&#39;ccc&#39;,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!

Statement:
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