[英]How to change WebView content on Button click in Android?
I am implementing a small project in which I'm diplaying a local image in my webview (in order to use the Zooming & Panning functionality), and on a button click the Next Image will be loaded. 我正在实施一个小项目,其中在Web视图中显示本地图像(以使用“缩放和平移”功能),然后在按钮上单击“下一个图像”。 I'm able to load the image, but on the Button click I'm unable to reload the webview with the new image.
我可以加载图片,但是在“按钮”上单击“我无法使用新图片重新加载webview”。
Any suggestions will really helpful. 任何建议都将真正有帮助。 :)
:)
Below is my WebViewActivity.java code: 下面是我的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;
}
}
Following is the web_view_activity.xml 以下是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");
}
});
You need to handle the url array increment and decrements manually. 您需要手动处理url数组的增量和减量。
This question can help you problem: android how to create a try it yourself editor 这个问题可以帮助您解决问题: android如何创建自己尝试的编辑器
You can dynamicly prepare and upload data to your WebView (or set url from net) 您可以动态准备数据并将其上传到WebView(或从net设置url)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.