【问题标题】:How to send data from activity to a webview in that activity itself?如何将数据从活动发送到该活动本身的网络视图?
【发布时间】:2015-08-22 08:59:50
【问题描述】:

我正在从这个 Activity 向另一个 Activity 发送数据

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final EditText etxt=(EditText)findViewById(R.id.editText1);
            final EditText etxt1=(EditText)findViewById(R.id.editText2);
    Button btn=(Button)findViewById(R.id.button1);
    btn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            String s= etxt.getText().toString();
                            String s1= etxt1.getText().toString();
            Intent intent= new Intent(getApplicationContext(),WebActivity.class);
            intent.putExtra("key", s);
                            intent.putExtra("key1", s1);
        }
    });
}
}

我的第二个活动是从 Intent 接收数据并希望将其显示在 web 视图中。

public class WebActivity extends Activity {
WebView webView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.webpage);
    Bundle b = getIntent().getExtras();
    String s = b.getString("key");
            String s1=b.getString("key1");
    webView=(WebView)findViewById(R.id.webview);

}

}

通过在 Google 上的一些搜索,我发现它可以通过使用 javascript 变量来完成,但我不知道该怎么做。请回答。

【问题讨论】:

    标签: javascript android android-webview


    【解决方案1】:

    您是否查看过 Android WebView 文档?

     // Simplest usage: note that an exception will NOT be thrown
     // if there is an error loading this page (see below).
     webview.loadUrl("http://slashdot.org/");
    
     // OR, you can also load from an HTML string:
     String summary = "<html><body>You scored <b>192</b> points.</body></html>";
     webview.loadData(summary, "text/html", null);
     // ... although note that there are restrictions on what this HTML can do.
     // See the JavaDocs for loadData() and loadDataWithBaseURL() for more info.
    

    【讨论】:

    • 假设,我想将 json 数据发送到 webview 呢?
    • 字符串摘要 = "你的 JSON 在这里"; webview.loadData(summary, "text/html", null);
    【解决方案2】:

    您可以简单地将数据添加到 webview 中

    webview.loadData("your string text", "text/html", null);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-28
      • 2017-03-03
      相关资源
      最近更新 更多