繁体   English   中英

用SeekBar的进度更改覆盖WebView

[英]Overwriting WebView with progress change of SeekBar

我创建了一个将google.com加载为测试的WebView。 在布局的底部,还有一个SeekBar。 如果Seekbar更改为2,则应加载另一个页面,例如stackoverflow.com。 如果其进度更改为3,则应加载另一个页面(android.com)

我现有的代码如下。 但是我现在不知道如何填补听众。 同样,使用现有解决方案也无法正常工作。

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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/Viewing"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />
    <SeekBar
        android:id="@+id/seitenSwitcher"
        android:layout_above="@+id/seitenSwitcher"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:max="15"
        android:progress="0"
        android:paddingLeft="4dip"
        android:paddingRight="4dip"

        />
</RelativeLayout>

main.java

package testprojekt.homepageapp.application;

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

public class main extends Activity {

    private WebView webv;


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


        webv = (WebView)findViewById(R.id.Viewing);
        webv.setWebViewClient(new WebViewClient());
        webv.getSettings().setJavaScriptEnabled(true);
        webv.loadUrl("http://www.google.de");
    }


    public void onProgressChanged(SeekBar seitenSwitcher, int progress, boolean true) {
        seitenSwitcher = (SeekBar) findViewById(R.id.seitenSwitcher);
        seitenSwitcher.setOnSeekBarChangeListener(new sbListener());

    }

}

sbListener.java

package testprojekt.homepageapp.application;

import android.widget.SeekBar;


public class sbListener implements SeekBar.OnSeekBarChangeListener {

   ???

}

您需要在侦听器中引用webv 您可以将其传递到sbListener的构造函数中,也可以删除sbListener类并执行以下操作:

public class main extends Activity {

    private WebView webv;
    private SeekBar seitenSwitcher;
    private String[] websites = { "http://www.google.de", "http://stackoverflow.com", "http://www.android.com" };

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

        webv = (WebView)findViewById(R.id.Viewing);
        webv.getSettings().setJavaScriptEnabled(true);
        webv.loadUrl(websites[0]);

        seitenSwitcher = (SeekBar) findViewById(R.id.seitenSwitcher);
        seitenSwitcher.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if (progress >= 0 && progress < websites.length) {
                    webv.loadUrl(websites[progress]);
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
    }

}

请注意,要使其正常工作,您需要在清单中具有以下权限:

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

我还略微修改了xml的布局,并将fill_parent更改为match_parent有关原因,请参见此处 ):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <WebView android:id="@+id/Viewing"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_above="@+id/seitenSwitcher"/>
    <SeekBar
        android:id="@+id/seitenSwitcher"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:max="15"
        android:progress="0"
        android:paddingLeft="4dp"
        android:paddingRight="4dp"
        />
</RelativeLayout>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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