我正在实现一个小项目,我在我的 web View 中显示本地镜像(以便使用缩放和平移功能),然后单击按钮将加载下一张图像。 我能够加载图像,但在单击按钮时我无法使用新图像重新加载 WebView 。
任何建议都会很有帮助。 :)
下面是我的 WebViewActivity.java 代码:
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.web_view_activity);
// Initialize all View Components
flippy = (ViewFlipper)findViewById(R.id.viewFlipper1);
WebView mWebView = (WebView) findViewById(R.id.webView);
next = (ImageButton)findViewById(R.id.next);
next.setAlpha(100);
previous = (ImageButton)findViewById(R.id.previous);
previous.setAlpha(100);
mWebView.setInitialScale(0);
WebSettings webSettings = mWebView.getSettings();
webSettings.setUseWideViewPort(true);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl("file:///android_asset/start.png");
mWebView.getSettings().setBuiltInZoomControls(true);
}
public void onClickFeature (View v)
{
int id = v.getId ();
switch (id) {
case R.id.next :
imagename = /*nextImagename*/;
mWebView.loadUrl(imagename)
flippy.showNext();
break;
case R.id.previous:
imagename = /*previousImagename*/;
mWebView.loadUrl(imagename)
flippy.showNext();
break;
default:
break;
}
}
以下是web_view_activity.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ViewFlipper
android:id="@+id/viewFlipper1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<WebView
android:id="@+id/webView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</ViewFlipper>
<ImageButton
android:id="@+id/previous"
android:layout_width="59dp"
android:layout_height="wrap_content"
android:layout_gravity="center|left"
android:background="@null"
android:onClick="onClickFeature"
android:src="@drawable/previous" />
<ImageButton
android:id="@+id/next"
android:layout_width="59dp"
android:layout_height="wrap_content"
android:layout_gravity="center|right"
android:background="@null"
android:onClick="onClickFeature"
android:src="@drawable/next" />
最佳答案
next.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
WebViewActivity.this.mWebView.loadUrl("your url array");
}
});
您需要手动处理 url 数组的递增和递减。
关于android - 如何更改 Android 中 Button 单击时的 WebView 内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12263991/