Android: How to open the webview in a new screen

大城市里の小女人 提交于 2020-01-15 07:47:09

问题


I've got four Image Buttons in screen1.xml. I've defined the webview in screen3.xml. If i click the ImageButton1 or other buttons, the repective URL (in webview) should load in screen3.xml.

I tried to do this, but it throws forceclose error. But If I try the same this with webview defined in screen1.xml, it works. But the problem is the Image Buttons and the webview appears in the same page and they are overlapped which looks awful!

Please help me out to open the webview in a new screen.

BTW, can addView be used for this purpose?

public class click extends Activity 
{
 private WebView webView;
    public void onCreate(Bundle savedInstanceState) 
    {
     super.onCreate(savedInstanceState);
        setContentView(R.layout.screen1);

        webView = (WebView) findViewById(R.id.web_view);
        ImageButton i1 = (ImageButton) findViewById(R.id.imagebutton1);
        ImageButton i2 = (ImageButton) findViewById(R.id.imagebutton2);
        ImageButton i3 = (ImageButton) findViewById(R.id.imagebutton3);
        ImageButton i4 = (ImageButton) findViewById(R.id.imagebutton4);
 }
private void openBrowser1() 
     {

      webView.getSettings().setJavaScriptEnabled(true);

      webView.loadUrl("myurl");
     }

public void myClickHandler1(View view) 
        {

         openBrowser1();
        }

Screen1.xml

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

  <WebView
android:id="@+id/web_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0" />



 <ImageButton android:id="@+id/imagebutton1" android:clickable="true" android:onClick="myClickHandler1" 
 android:layout_width="130dip" android:background="@null" android:layout_height="130dip" android:layout_x="21dip" 
 android:layout_y="61dip" ></ImageButton>

 <ImageButton android:id="@+id/imagebutton2" android:clickable="true" android:onClick="myClickHandler2" 
 android:layout_width="130dip" android:background="@null"  android:layout_height="130dip" android:layout_x="171dip" 
 android:layout_y="61dip" ></ImageButton>

 <ImageButton android:id="@+id/imagebutton3" android:clickable="true" android:onClick="myClickHandler3" 
 android:layout_width="130dip" android:background="@null" android:layout_height="130dip" android:layout_x="21dip" 
 android:layout_y="241dip" ></ImageButton>

 <ImageButton android:id="@+id/imagebutton4" android:clickable="true" android:onClick="myClickHandler4" 
 android:layout_width="130dip" android:background="@null"  android:layout_height="130dip" android:layout_x="171dip" 
 android:layout_y="241dip" ></ImageButton>
</AbsoluteLayout> 

screen3.xml

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >                 

<WebView
android:id="@+id/web_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0" />

</AbsoluteLayout> 

回答1:


I think you need some fix to your code...

public class click extends Activity {

    private WebView webView;    

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.screen1);

        webView = (WebView) findViewById(R.id.web_view);        
        ImageButton i1 = (ImageButton) findViewById(R.id.imagebutton1);
        ImageButton i2 = (ImageButton) findViewById(R.id.imagebutton2);
        ImageButton i3 = (ImageButton) findViewById(R.id.imagebutton3);
        ImageButton i4 = (ImageButton) findViewById(R.id.imagebutton4);

        WebSettings webSettings = webView.getSettings();
        webSettings.setSavePassword(false);
        webSettings.setSaveFormData(false);
        webSettings.setJavaScriptEnabled(true);
        webSettings.setSupportZoom(false);
        webView.loadUrl("http://www.reforma.com");

        //Add a listener to ImageButton1
        i1.setOnClickListener(new OnClickListener() {                       
            @Override
            public void onClick(View v) {
                openBrowser1();
            }           
        });                             

    }

    private void openBrowser1() 
    {       
       //this intent is used to open other activity wich contains another webView
        Intent intent = new Intent(click.this,SecondActivity.class);
        startActivity(intent); 
    }

}

screen1.xml

  <WebView
android:id="@+id/web_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />

 <ImageButton android:id="@+id/imagebutton1" android:clickable="true"  
 android:layout_width="130dip" android:layout_height="130dip" android:layout_x="21dip" 
 android:layout_y="61dip" ></ImageButton>

 <ImageButton android:id="@+id/imagebutton2" android:clickable="true"  
 android:layout_width="130dip" android:layout_height="130dip" android:layout_x="171dip" 
 android:layout_y="61dip" ></ImageButton>

 <ImageButton android:id="@+id/imagebutton3" android:clickable="true"  
 android:layout_width="130dip" android:layout_height="130dip" android:layout_x="21dip" 
 android:layout_y="241dip" ></ImageButton>

 <ImageButton android:id="@+id/imagebutton4" android:clickable="true"  
 android:layout_width="130dip"  android:layout_height="130dip" android:layout_x="171dip" 
 android:layout_y="241dip" ></ImageButton>
</AbsoluteLayout> 

Add a new activity wich contains your webview where the webpage should be load!.

public class SecondActivity extends Activity {

    private WebView webView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.screen3);

        webView = (WebView) findViewById(R.id.web_view);
        WebSettings webSettings = webView.getSettings();
        webSettings.setSavePassword(false);
        webSettings.setSaveFormData(false);
        webSettings.setJavaScriptEnabled(true);
        webSettings.setSupportZoom(false);
        webView.loadUrl("http://www.reforma.com");         

    }

 }

screen3.xml

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >                 

<WebView
android:id="@+id/web_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />

</AbsoluteLayout> 

Add to your AndroidManifest.xml your second Activity

   <activity android:name=".SecondActivity"
          android:label="SecondActivity"/> 

and you must have Internet permission to load the web page...

<uses-permission android:name="android.permission.INTERNET"></uses-permission>

any doubts???

Jorgesys




回答2:


It doesn't look like you're inflating and loading the screen3.xml layout anywhere, so attempts to reference the WebView it contains will probably generate an exception. Sounds like what you really want to do is define another Activity for the webview layout and swap to it when the user pushes one of the buttons. See the 2nd part of the Notepad sample for a description of how to add additional Activities to an application and swap between them:

http://developer.android.com/resources/tutorials/notepad/notepad-ex2.html



来源:https://stackoverflow.com/questions/4857303/android-how-to-open-the-webview-in-a-new-screen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!