如何将活动中的数据发送到该活动本身的webview中?

时间:2013-05-13 09:11:56

标签: javascript android android-webview

从此活动我将数据发送到另一个活动

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);
        }
    });
}
}

我的第二个活动,我从意图中收到数据,并希望在网页浏览中显示。

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);

}

}

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

2 个答案:

答案 0 :(得分: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.

答案 1 :(得分:0)

您只需在网页浏览中添加数据

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