最近帮同学做了小的程序,就是页面可滑动,切换到下一个网站地址。
以下是我做这个demo的的步骤以及相关代码:
1. 首先讲一个网站内容通过手机显示,而不是跳转到浏览器。
使用webView,将内容进行导入。
1. 在"AndroidManifest.xml"添加访问互联网权限
<uses-permission android:name="android.permission.INTERNET" />
2)新创建一个layout文件中添加webview这个控件。
web_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<WebView
android:id="@+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
3)创建一个自己的WebViewClient,代码如下:
4) 使用loadUrl方法导入你要显示的网站URL.
webView.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode,String description, String failingUrl) { // Handle the error
}
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
补充:如果想要跳转手机浏览器:步骤如下:
1. 在"AndroidManifest.xml"添加访问互联网权限
<uses-permission android:name="android.permission.INTERNET" />
2. 添加跳转代码:
Uri uri = Uri.parse("https://www.baidu.com/");
Intent it = new Intent(Intent.ACTION_VIEW, uri);
startActivity(it);
2.添加手势相关代码。
这里主要运用的是ViewFlipper结合GestureDetector
1.在activity_main添加ViewFlipper控件
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.appporject.MainActivity" >
<ViewFlipper
android:id="@+id/viewFlipper"
android:layout_width="match_parent"
android:layout_height="match_parent"
></ViewFlipper>
</RelativeLayout>
private GestureDetector mGestureDetector;,然后进行初始化。
viewFlipper.setOnTouchListener(this);
mGestureDetector = new GestureDetector(this);
3.实现onTouch事件。
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_MOVE:
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_UP:
break;
}
mGestureDetector.onTouchEvent(event);
return true;
}
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_MOVE:
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_UP:
break;
}
mGestureDetector.onTouchEvent(event);
return true;
}
4. 处理onFling()函数,我们获得瞬间滑动后回调onFling()这个抽象函数。用于确定跳转到哪一页。
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
int x = (int) (e2.getX() - e1.getX());//计算滑动距离,用于区别是翻上页还是下页
if (x > 0) {
movePrevious();
} else {
moveNext();
}
return false;
}
5.实现webView导入网页,需要记录当前页和前一页
private void setView(int curr, int next) {
View v = (View) mInflater.inflate(R.layout.web_view, null);
if (curr < next && next > urls.length - 1)
next = 0;
else if (curr > next && next < 0)
next = urls.length - 1;
WebView webView = (WebView) v.findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setSupportZoom(true);
webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
activity.setTitle("Loading...");
activity.setProgress(progress * 100);
if (progress == 100) {
activity.setTitle(R.string.app_name);
}
}
});
webView.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) { // Handle the error
}
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
webView.loadUrl(urls[next]);//数组,用于存放url
if (viewFlipper.getChildCount() > 1) {
viewFlipper.removeViewAt(0);
}
viewFlipper.addView(v, viewFlipper.getChildCount());
mCurrPos = next;
}
以上就是相关代码,稍微有些乱。