弹来弹去跑马灯!

temp-2017-4-20

-------
uuuuuuuuuuu

package com.wgscd.gwang.myapplication;

/**

  • Created by gwang on 2017/3/14.
    */

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;

import org.json.JSONObject;

public class JsOperator {

private Context context;

public JsOperator(Context context) {
    this.context = context;
}

/**
 * 弹出消息对话框
 */
@JavascriptInterface
public void showDialog(String message) {

    AlertDialog.Builder builder = new Builder(context);
    builder.setMessage(message);
    builder.setTitle("提示");
    builder.setPositiveButton("确认", new OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {

        }
    });
    builder.setNegativeButton("取消", new OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
        }
    });
    builder.create().show();
}

@JavascriptInterface
public void getPageResult(String pageNumber) {

    AlertDialog.Builder builder = new Builder(context);
    builder.setMessage("" + pageNumber);
    builder.setTitle("提示");
    builder.setPositiveButton("确认", new OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {

        }
    });
    builder.setNegativeButton("取消", new OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
        }
    });
    builder.create().show();
}

@JavascriptInterface
public  static void     showAndroid(Activity act,final WebView webView, String msg){
    final String info = "来自手机内的内容!!!";
    act.runOnUiThread(new Runnable(){
        @Override
        public void run() {
            webView.loadUrl("javascript:add('"+info+"')");
        }
    });
}


/**
 * 获取登录的用户名和密码
 * @return JSON格式的字符串
 */
@JavascriptInterface
public String getLoginInfo(){
    try{
        JSONObject login = new JSONObject();
        login.put("Username", "YLD");
        login.put("Password", "111");

        return login.toString();
    }catch(Exception e){
        e.printStackTrace();
    }

    return null;
}

}

----------MainActivity---------------------
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button) findViewById(R.id.btn1);
img = (ImageView) findViewById(R.id.img);
img2 = (ImageView) findViewById(R.id.img2);
panelLeft=(LinearLayout) findViewById(R.id.panelLeft) ;

    web=(WebView) findViewById(R.id.web);
    web.getSettings().setDefaultTextEncodingName("UTF-8") ;
    web.getSettings().setJavaScriptEnabled(true);
    web.getSettings().setAllowFileAccess(true);
    web.addJavascriptInterface(new JsOperator(MainActivity.this),"JsInteraction");
    //web.loadData("<html><body color='red'>test<input type='button' value='click me' onclick='showMsg()' /></body><script type=\"text/javascript\">function showMsg(){JsInteraction.showDialog(\"Login start...\");}</script></html>", "text/html", "UTF-8");
   // web.loadData( getFromAssets("pageListTemplate.txt"), "text/html", "UTF-8");
    String s = getFromAssets("pageListTemplate.txt");//read text from Assets
    web.loadData( s, "text/html", "UTF-8");
    Toast.makeText(MainActivity.this, "s:" + s, Toast.LENGTH_SHORT).show();


    //shareMsg("active ttt","ttttttt","test http://www.qq.com/   测试","/assets/1.jpg");

    btn1.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            FrameLayout.LayoutParams ll = new FrameLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
             int w=-128;
             ll.setMargins(80,  0, 0, 0);
            panelLeft.setVisibility(View.INVISIBLE);
            // panelLeft.setLayoutParams(ll);
           //  panelLeft.startAnimation( a2);

           // JsOperator.showAndroid(act,web,"<p>g----------</p>");
            sData="'<br>ffgfgff<p>ddddddddddddddddd</p>'";
            web.loadUrl("javascript:add("+ sData +")");

        }
    });



/**
 * 从Assets中读取图片
 */
private Bitmap getImageFromAssetsFile(String fileName)
{
    Bitmap image = null;
    AssetManager am = getResources().getAssets();
    try
    {
        InputStream is = am.open(fileName);
        image = BitmapFactory.decodeStream(is);
        is.close();
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }

    return image;

}

//从Assets中读取text

public String getFromAssets(String fileName){
    String line="";
    String Result="";
    try {
        InputStreamReader inputReader = new InputStreamReader( getResources().getAssets().open(fileName) );
        BufferedReader bufReader = new BufferedReader(inputReader);

        while((line = bufReader.readLine()) != null)
            Result += line;

    } catch (Exception e) {
        e.printStackTrace();
    }
    return Result;
}

}

posted @ 2017-04-20 16:36  wgscd  阅读(148)  评论(3编辑  收藏  举报