[英]Overwriting WebView with progress change of SeekBar
I created a WebView which loads google.com as test. 我创建了一个将google.com加载为测试的WebView。 And at the bottom of the Layout ist a SeekBar. 在布局的底部,还有一个SeekBar。 If the Seekbar is changed to progress of 2 it shall for example load another page like stackoverflow.com. 如果Seekbar更改为2,则应加载另一个页面,例如stackoverflow.com。 If its progress is changed to 3,it shall load another page ( android.com) 如果其进度更改为3,则应加载另一个页面(android.com)
My existing code is the following. 我现有的代码如下。 But I dont know how to fill the listener now. 但是我现在不知道如何填补听众。 Also with existing solutions it did not work. 同样,使用现有解决方案也无法正常工作。
activity_main.xml 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 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 sbListener.java
package testprojekt.homepageapp.application;
import android.widget.SeekBar;
public class sbListener implements SeekBar.OnSeekBarChangeListener {
???
}
You need a reference to webv
in the listener. 您需要在侦听器中引用webv
。 You can pass it into the constructor of sbListener
, or you can remove the sbListener
class and do something like: 您可以将其传递到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) {
}
});
}
}
Note, to get this to work you will require the following permissions in your manifest: 请注意,要使其正常工作,您需要在清单中具有以下权限:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
I also modified the layout of xml slightly, and changed fill_parent
to match_parent
( see here for why ): 我还略微修改了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.