Android: как открыть веб-просмотр на новом экране - PullRequest
2 голосов
/ 01 февраля 2011

У меня есть четыре кнопки изображения в screen1.xml. Я определил веб-просмотр в screen3.xml. Если я нажимаю кнопку ImageButton1 или другие кнопки, соответствующий URL-адрес (в веб-представлении) должен загружаться в screen3.xml.

Я пытался сделать это, но выдает ошибку принудительного закрытия. Но если я попробую то же самое с webview, определенным в screen1.xml, это сработает. Но проблема в том, что кнопки «Изображение» и веб-представление отображаются на одной странице, и они перекрываются, что выглядит ужасно!

Пожалуйста, помогите мне открыть веб-просмотр на новом экране.

Кстати, можно ли для этой цели использовать 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();
        }

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> 

Ответы [ 2 ]

4 голосов
/ 01 февраля 2011

Я думаю, вам нужно исправить код ...

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> 

Добавьте в свой AndroidManifest.xml свою вторую активность

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

и вам необходимоесть интернет-разрешение на загрузку веб-страницы ...

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

есть сомнения ???

Jorgesys

1 голос
/ 01 февраля 2011

Не похоже, что вы надуваете и загружаете макет screen3.xml в любом месте, поэтому попытки обратиться к содержащемуся в нем WebView, вероятно, вызовут исключение. Похоже, что вы действительно хотите сделать, это определить другое действие для макета веб-просмотра и переключиться на него, когда пользователь нажмет одну из кнопок. См. Вторую часть примера «Блокнот» для описания того, как добавить дополнительные действия в приложение и поменять их местами:

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

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...