如何在edittext中传递webview url中的字符串

时间:2013-11-26 08:08:55

标签: android android-intent webview

我在MainActivity中有两个活动MainActivity.java和WebViewActivity.java,它有一个edittext和一个按钮。现在我想如果我在edittext中输入任何字符串,并且在按钮单击后它应该将该字符串传递到WebViewActivity中的url,如www.mywebsite.com + getTestFromEdittext

MainActivity.java

super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    EditText inputUrl = (EditText) findViewById(R.id.editText1);

    Button button = (Button)findViewById(R.id.button1);
    button.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                       Intent intent = new Intent(MainActivity.this, WebViewActivity.class);
                       startActivity(intent);
                }
            });

}

}

WebViewActivity.java

protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.php);
    WebView wb = (WebView)findViewById(R.id.webview);
    wb.loadUrl("http://example.com" + getStringFromEdittext);
    wb.getSettings().setJavaScriptEnabled(true);

}

}

1 个答案:

答案 0 :(得分:2)

启动活动时使用此代码:

MainActivity:

Intent intent = new Intent(MainActivity.this, WebViewActivity.class);
intent.putExtra("text", inputUrl.getText().toString());
startActivity(intent);

获取值:

WebViewActivity:

String getStringFromEdittext = getIntent().getExtras().getInt("text");