Android的:如何在一个新屏幕中打开网页视图 [英] Android: How to open the webview in a new screen

查看:139
本文介绍了Android的:如何在一个新屏幕中打开网页视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有在screen1.xml四个图像按钮。
我定义在screen3.xml web视图。
如果我点击ImageButton1或其他按键,repective UR​​L(在web视图)应screen3.xml加载。

我试图做到这一点,但它抛出forceclose错误。但如果我尝试同样这与screen1.xml定义web视图,它的工作原理。但问题是图像按钮和web视图显示在同一页面,它们重叠,看起来太可怕了!

请帮我在一个新的屏幕打开网页视图。

顺便说一句,可以ad​​dView被用于此目的?

 公共类点击扩展活动
{
 私人的WebView web视图;
    公共无效的onCreate(捆绑savedInstanceState)
    {
     super.onCreate(savedInstanceState);
        的setContentView(R.layout.screen1);        web视图=(的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);
 }
私人无效openBrowser1()
     {      webView.getSettings()setJavaScriptEnabled(真)。      webView.loadUrl(myurl);
     }公共无效myClickHandler1(查看视图)
        {         openBrowser1();
        }

Screen1.xml

 <?XML版本=1.0编码=UTF-8&GT?;
< AbsoluteLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:方向=垂直
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT>  <的WebView
机器人:ID =@ + ID / web_view
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_weight =1.0/> < ImageButton的机器人:ID =@ + ID / imagebutton1机器人:可点击=真正的机器人:的onClick =myClickHandler1
 机器人:layout_width =130dip机器人:背景=@空机器人:layout_height =130dip机器人:layout_x =21dip
 机器人:layout_y =61dip>< / ImageButton的> < ImageButton的机器人:ID =@ + ID / imagebutton2机器人:可点击=真正的机器人:的onClick =myClickHandler2
 机器人:layout_width =130dip机器人:背景=@空机器人:layout_height =130dip机器人:layout_x =171dip
 机器人:layout_y =61dip>< / ImageButton的> < ImageButton的机器人:ID =@ + ID / imagebutton3机器人:可点击=真正的机器人:的onClick =myClickHandler3
 机器人:layout_width =130dip机器人:背景=@空机器人:layout_height =130dip机器人:layout_x =21dip
 机器人:layout_y =241dip>< / ImageButton的> < ImageButton的机器人:ID =@ + ID / imagebutton4机器人:可点击=真正的机器人:的onClick =myClickHandler4
 机器人:layout_width =130dip机器人:背景=@空机器人:layout_height =130dip机器人:layout_x =171dip
 机器人:layout_y =241dip>< / ImageButton的>
< / AbsoluteLayout>

screen3.xml

 <?XML版本=1.0编码=UTF-8&GT?;
< AbsoluteLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:方向=垂直
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    ><的WebView
机器人:ID =@ + ID / web_view
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_weight =1.0/>< / AbsoluteLayout>


解决方案

我想你需要一些修复你的code ...

 公共类点击扩展活动{    私人的WebView web视图;    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.screen1);        web视图=(的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(假);
        webSettings.setSaveFor​​mData(假);
        webSettings.setJavaScriptEnabled(真);
        webSettings.setSupportZoom(假);
        webView.loadUrl(http://www.reforma.com);        //添加一个监听ImageButton1
        i1.setOnClickListener(新OnClickListener(){
            @覆盖
            公共无效的onClick(视图v){
                openBrowser1();
            }
        });    }    私人无效openBrowser1()
    {
       //这个意图是用来打开其他活动至极包含另一个web视图
        意向意图=新意图(click.this,SecondActivity.class);
        startActivity(意向);
    }}

screen1.xml


    

 <的WebView
机器人:ID =@ + ID / web_view
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_weight =1/> < ImageButton的机器人:ID =@ + ID / imagebutton1机器人:可点击=真
 机器人:layout_width =130dip机器人:layout_height =130dip机器人:layout_x =21dip
 机器人:layout_y =61dip>< / ImageButton的> < ImageButton的机器人:ID =@ + ID / imagebutton2机器人:可点击=真
 机器人:layout_width =130dip机器人:layout_height =130dip机器人:layout_x =171dip
 机器人:layout_y =61dip>< / ImageButton的> < ImageButton的机器人:ID =@ + ID / imagebutton3机器人:可点击=真
 机器人:layout_width =130dip机器人:layout_height =130dip机器人:layout_x =21dip
 机器人:layout_y =241dip>< / ImageButton的> < ImageButton的机器人:ID =@ + ID / imagebutton4机器人:可点击=真
 机器人:layout_width =130dip机器人:layout_height =130dip机器人:layout_x =171dip
 机器人:layout_y =241dip>< / ImageButton的>
< / AbsoluteLayout>

添加一个新的活动至极包含您的WebView其中网页应该负荷!

 公共类SecondActivity延伸活动{    私人的WebView web视图;
    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.screen3);        web视图=(的WebView)findViewById(R.id.web_view);
        WebSettings webSettings = webView.getSettings();
        webSettings.setSavePassword(假);
        webSettings.setSaveFor​​mData(假);
        webSettings.setJavaScriptEnabled(真);
        webSettings.setSupportZoom(假);
        webView.loadUrl(http://www.reforma.com);    } }

screen3.xml

 <?XML版本=1.0编码=UTF-8&GT?;
< AbsoluteLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:方向=垂直
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    ><的WebView
机器人:ID =@ + ID / web_view
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_weight =1/>< / AbsoluteLayout>

添加到AndroidManifest.xml你的第二个活动

 <活动机器人:名字=。SecondActivity
          机器人:标签=SecondActivity/>

和您必须具有Internet权限才能加载网页...

 <使用许可权的android:NAME =android.permission.INTERNET对>< /使用许可权>

任何疑问???

Jorgesys

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> 

解决方案

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

这篇关于Android的:如何在一个新屏幕中打开网页视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆