温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

安卓开发之android使用webview时按后退会退出而不是回退的问题

发布时间:2020-07-28 20:02:20 阅读:6094 作者:jiyue0520_624 栏目:移动开发
Android开发者专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

最近公司使用android打包html5游戏,

使用android点webview打包html5游戏,代码都好了,具体如下

public class MainActivity extends ActionBarActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);
         WebView webView=new WebView(this);
        webView.getSettings().setJavaScriptEnabled(true);//支持js        webView.loadUrl("http://www.ysmall.cc/mobile");
        webView.setWebViewClient(new MyWebViewClient());


    }    @Override    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.menu_main, menu);        return true;
    }    @Override    public boolean onOptionsItemSelected(MenuItem item) {        // Handle action bar item clicks here. The action bar will        // automatically handle clicks on the Home/Up button, so long        // as you specify a parent activity in AndroidManifest.xml.        int id = item.getItemId();        //noinspection SimplifiableIfStatement        if (id == R.id.action_settings) {            return true;
        }        return super.onOptionsItemSelected(item);
    }
}

一切貌似很完美,玩游戏,被电脑打败了(我真菜!!!),游戏提示失败,点击“更多游戏”去查看其他游戏,点进去后,然后点返回键,问题来了,不是挖掘技术哪家强,是webview没有后退功能,没法退回首页,而是直接退出,android开发问题还真多啊,还好哥机灵,找资料呗!

终于找到问题了,

只需要重写activity的onKeyDown方法,在onKeyDown里后退即可

全部代码贴出,免得以后忘记

bingo

与君共勉~

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI

开发者交流群×