[英]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.