在背部按下不起作用

时间:2016-04-05 17:12:02

标签: android

这是我用来通过Imagebutton打开webview的MainActivity。

但我需要整合“背压功能”

MainActivity.java

package com.example.metehan.hbc;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;

import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageButton;

public class MainActivity extends Activity {
    private WebView webView;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final WebView wb = (WebView) findViewById(R.id.webviewactivity_webview);
        wb.setVisibility(View.INVISIBLE);
        final ImageButton ib = (ImageButton) findViewById(R.id.webviewactivity_imagebutton);
        ib.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                wb.loadUrl("https://elearns02.fh-biberach.de/ilias3/login.php?target=&soap_pw=&ext_uid=&cookies=nocookies&client_id=HSBC&lang=de");
            }
        });
        wb.setWebViewClient(new WebViewClient() {
            public void onPageFinished(WebView view, String url) {
                ib.setVisibility(View.INVISIBLE);
                wb.setVisibility(View.VISIBLE);
            }
        });
    }}

1 个答案:

答案 0 :(得分:0)

如果您从startActivityForResult打开了新活动,则可以拨打finish()

如果这不起作用,那么您的AndroidManifest.xml中的android:noHistory = "true"设置为android:noHistory = "false"

如果上述方法无效,请在切换到新活动之前尝试在主要活动中调用finish()

对于您的情况,请尝试:

package com.example.metehan.hbc;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;

import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageButton;

public class MainActivity extends Activity {
    private WebView webView;


    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final WebView wb = (WebView) findViewById(R.id.webviewactivity_webview);
        wb.setVisibility(View.INVISIBLE);
        final ImageButton ib = (ImageButton) findViewById(R.id.webviewactivity_imagebutton);
        ib.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                wb.loadUrl("https://elearns02.fh-biberach.de/ilias3/login.php?target=&soap_pw=&ext_uid=&cookies=nocookies&client_id=HSBC&lang=de");
            }
        });
        wb.setWebViewClient(new WebViewClient() {
            public void onPageFinished(WebView view, String url) {
                ib.setVisibility(View.INVISIBLE);
                wb.setVisibility(View.VISIBLE);
            }
        });
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (event.getAction() == KeyEvent.ACTION_DOWN) {
            switch (keyCode) {
                case KeyEvent.KEYCODE_BACK:
                    if (webView.canGoBack()) {
                        webView.goBack();
                    } else {
                        finish();
                    }
                    return true;
            }

        }
        return super.onKeyDown(keyCode, event);
    }
    }}