How to pass string in webview url from edittext

2019-08-30 11:29发布

I have two activity MainActivity.java and WebViewActivity.java in MainActivity there is a edittext and a button. Now i want if i enter any string in edittext and after button click it should pass that string into url in WebViewActivity like 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条回答
Deceive 欺骗
2楼-- · 2019-08-30 12:19

Use this code while starting activity:

MainActivity :

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

To fetch the value :

WebViewActivity :

String getStringFromEdittext = getIntent().getExtras().getInt("text");
查看更多
登录 后发表回答