android - 如何更改 Android 中 Button 单击时的 WebView 内容?

标签 android android-webview

我正在实现一个小项目,我在我的 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/

相关文章:

android - 带有 SD 卡的平板电脑上的 Linux?

android - 页面加载后如何触发对 WebView 中文本输入字段的关注?

android - 选择 [CSS, JS, android] 时阻止标准操作

Android webview 空白 - Android 28 SDK

android - 如何仅从 WebViewClient 的新 onReceivedError 中的主页检测错误

javascript - 如何强制 Android 互联网浏览器立即显示样式更改

android - Dagger 2,在模块中提供应用上下文

android - 如何在android中使用getSharedPreferences

Android - 应用程序 (apk) 最大大小

java - 如何检查我们是否应该去谷歌搜索或只是简单的加载网址