简体   繁体   中英

Java android webview with a dynamic URL

Good afternoon,

I am quite new to the android development, so please excuse me if my question sounds stupid.

I have created a first layout "main" which consists of a textbox and and a button. The goal of the button is to launch the second layout containing the webview.

What I would like is that when I click on the button, the layout passes the value of the text box (which contains the URL as parameter) to the second layout instead of having a hard-coded URL)

I have been googling, searching for quite a while but so far, not very successful in my searches. Any idea?

Many thanks in advance for your kind help.

// Contents of 'laurent/ch/domoos/MainActivity.java' :

package laurent.ch.domoos;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;

public class MainActivity extends Activity {


private Button button;

public void onCreate(Bundle savedInstanceState) {
    final Context context = this;

    super.onCreate(savedInstanceState);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.main);



    button = (Button) findViewById(R.id.buttonUrl);

    button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            Intent intent = new Intent(context, WebViewActivity.class);

            startActivity(intent);
        }

    });

}

}

// Contents of laurent/ch/domoos/WebViewActivity.java' :

package laurent.ch.domoos;

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class WebViewActivity extends Activity  {

String myUrl = "http://192.168.X.YY/Domoos/";
String myUrl2 = "";
private WebView webView;

public void defineURL(String pUrl) {
   this.myUrl2 = pUrl;
}

public void onCreate(Bundle savedInstanceState) {



    super.onCreate(savedInstanceState);
   /* setContentView(R.layout.webview);

    webView = (WebView) findViewById(R.id.webView1);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.loadUrl(myUrl);
*/
    setContentView(R.layout.webview);

    //webview use to call own site
    webView =(WebView)findViewById(R.id.webView1);

    webView.setWebViewClient(new WebViewClient());
    webView .getSettings().setJavaScriptEnabled(true);
    webView .getSettings().setDomStorageEnabled(true);
    webView.loadUrl(myUrl);

}

}
  1. Create object of your EditText like you made it for Button
  2. Pass EditText value in Intent

     intent.putExtra("url",edt.getText().toString()); 
  3. Access this value in your WebviewActivity onCreate()

     String url=getIntent().getStringExtra("url"); 
  4. Load this url in your WebView .

     webView.loadUrl(url); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM