Android:如何在新屏幕中打开WebView

发布于 2024-10-15 07:49:11 字数 3138 浏览 3 评论 0原文

我在 screen1.xml 中有四个图像按钮。 我已经在 screen3.xml 中定义了 webview。 如果我单击 ImageButton1 或其他按钮,相应的 URL(在 web 视图中)应加载到 screen3.xml 中。

我尝试这样做,但它引发了强制关闭错误。但是如果我尝试使用 screen1.xml 中定义的 webview 进行相同的操作,它会起作用。但问题是图像按钮和网络视图出现在同一页面中,并且它们重叠,看起来很糟糕!

请帮助我在新屏幕中打开网络视图。

顺便说一句,addView 可以用于此目的吗?

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

屏幕1.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> 

屏幕3.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> 

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> 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

箜明 2024-10-22 07:49:11

我认为你需要对你的代码进行一些修复...

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> 

添加一个新活动,其中包含应加载网页的网页视图!

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> 

将您的第二个 Activity 添加到您的 AndroidManifest.xml

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

,并且您必须具有 Internet 权限才能加载网页...

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

有任何疑问吗???

约热西斯

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

2024-10-22 07:49:11

它看起来不像您在任何地方膨胀和加载 screen3.xml 布局,因此尝试引用它包含的 WebView 可能会生成异常。听起来你真正想做的是为 webview 布局定义另一个 Activity 并在用户按下其中一个按钮时交换到它。请参阅记事本示例的第二部分,了解如何向应用程序添加其他活动并在它们之间进行交换的说明:

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文