Android 开发之webview页面返回到最顶关闭



public class MyWebViewActivity extends Activity {
 private Context context = this;
 // 底部菜单View
// public static View bottomView;
 private WebViewProgressBar webView;
 private String url;
 private HtmlBean bean;
 // 手势
 private GestureDetector mGestureDetector;
 private TextView btn_goback;
 String title;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);
  requestWindowFeature(Window.FEATURE_NO_TITLE);
  setContentView(R.layout.usershop_layout);
  MyUtils.checkStrictMode(true);
  
  url = getIntent().getStringExtra("url");
  if (!TextUtils.isEmpty(title)) {
   TextView tv_title = (TextView) findViewById(R.id.title_text);
   if (title.length() > 6) {
    title = title.substring(0, 6);
   }
   tv_title.setText(title);
  }
  if (TextUtils.isEmpty(url)) {
   Toast.makeText(this, "没有找到相关的链接地址", Toast.LENGTH_LONG).show();
   return;
  }

  initWebView();
  this.initJsInterface();
  initGestureDetector();
 }

 // 初始化webView
 private void initWebView() {
  // 底部菜单
//  bottomView = findViewById(R.id.user_bottom);
  btn_goback=(TextView)findViewById(R.id.btn_goback);
  // WebView
  webView = (WebViewProgressBar) findViewById(R.id.user_webview);
  // 滚动条
  webView.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);
  // 脚本支持
  webView.getSettings().setJavaScriptEnabled(true);
  // 缩放
  webView.getSettings().setSupportZoom(true);
  webView.loadUrl(url);

  // WebView客户端
  webView.setWebViewClient(new WebViewClient() {
   @Override
   public boolean shouldOverrideUrlLoading(WebView view, String nextUrl) {
    // 下一个链接
    try {
     if(title.equals("个人中心")){
      url=nextUrl;
     }
    } catch (Exception e) {
    }
    view.loadUrl(nextUrl);
    return true;
   }

   // 开始加载
   @Override
   public void onPageStarted(WebView view, String url2, Bitmap favicon) {
    // TODO Auto-generated method stub
   }

   // 加载完毕
   @Override
   public void onPageFinished(WebView view, String url2) {
   }
  });
  btn_goback.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
    finish();
   }
  });
  // WebView的滑动事件交个手势处理
  webView.setOnTouchListener(new OnTouchListener() {
   @Override
   public boolean onTouch(View v, MotionEvent event) {
    // TODO Auto-generated method stub
    return mGestureDetector.onTouchEvent(event);
   }
  });
 }

 // 初始化手势
 private void initGestureDetector() {
  mGestureDetector = new GestureDetector(this, new OnGestureListener() {
   @Override
   public boolean onSingleTapUp(MotionEvent e) {
    // TODO Auto-generated method stub
    return false;
   }

   @Override
   public void onShowPress(MotionEvent e) {
    // TODO Auto-generated method stub

   }

   @Override
   public boolean onScroll(MotionEvent e1, MotionEvent e2,
     float distanceX, float distanceY) {
    // TODO Auto-generated method stub
    return false;
   }

   @Override
   public void onLongPress(MotionEvent e) {
    // TODO Auto-generated method stub

   }

   @Override
   public boolean onFling(MotionEvent e1, MotionEvent e2,
     float velocityX, float velocityY) {

    return false;
   }

   @Override
   public boolean onDown(MotionEvent e) {
    // TODO Auto-generated method stub
    return false;
   }
  }, new Handler() {

  });

 }

 @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  ShareDao.onActivityResult(requestCode, resultCode, data);
  super.onActivityResult(requestCode, resultCode, data);

 }

 public void btn_on(View view){
  if(webView.canGoBack()){
   webView.goBack(); // goBack()表示返回WebV
  }else {
   finish();
  }
 }

 }

}

你可能感兴趣的:(android)