zoukankan      html  css  js  c++  java
  • [android] 实现返回键操作思路

    记录用户点击的操作历史,使用栈数据结构,频繁的操作栈顶(添加,获取,删除),使用LinkedList

    捕获用户的返回键操作,响应返回键,返回上一个界面

    MainActivity.java

        /**
         * 返回键处理
         */
        @Override
        public boolean onKeyDown(int keyCode, KeyEvent event) {
            if(keyCode==KeyEvent.KEYCODE_BACK){
                boolean result=MiddleManager.getInstance().goBack();
                if(!result){
                    Toast.makeText(MainActivity.this, "是否退出应用", 1).show();
                }
                return false;
            }
            return super.onKeyDown(keyCode, event);
        }

    MiddleManager.java

        //操作历史栈
        private LinkedList<String> HISTORY=new LinkedList<String>();
        /**
         * 处理返回键
         * @return 
         */
        public boolean goBack() {
            //如果为空会抛异常
            if(HISTORY.size()>0){
                if(HISTORY.size()==1){
                    return false;
                }
                
                HISTORY.removeFirst();
                if(HISTORY.size()>0){
                    String key=HISTORY.getFirst();
                    BaseView targetView=VIEWCACHE.get(key);
                    mainContainer.removeAllViews();
                    View child=targetView.getView();
                    mainContainer.addView(child);
                    child.startAnimation(AnimationUtils.loadAnimation(mainContainer.getContext(), R.anim.left_to_right));
                    currentView=targetView;
                    return true;
                }
            }
            return false;
        }
        /**
         * 加载界面
         */
        public void loadView(Class<? extends BaseView> baseViewClass) {
            BaseView baseView=null;
            //判断是否存在
            String key=baseViewClass.getSimpleName();
            if(VIEWCACHE.containsKey(key)){
                baseView=VIEWCACHE.get(key);
            }else{
                try {
                    Constructor<? extends BaseView> constructor;
                    constructor = baseViewClass.getConstructor(Context.class);
                    baseView=constructor.newInstance(mainContainer.getContext());
                } catch (Exception e) {
                    e.printStackTrace();
                }
                VIEWCACHE.put(key, baseView);
            }
            mainContainer.removeAllViews();
            View child=baseView.getView();
            mainContainer.addView(child);
            child.startAnimation(AnimationUtils.loadAnimation(mainContainer.getContext(), R.anim.left_to_right));
            currentView=baseView;
            //放入栈
            HISTORY.addFirst(key);
        }
  • 相关阅读:
    BNU 51002 BQG's Complexity Analysis
    BNU OJ 51003 BQG's Confusing Sequence
    BNU OJ 51000 BQG's Random String
    BNU OJ 50999 BQG's Approaching Deadline
    BNU OJ 50998 BQG's Messy Code
    BNU OJ 50997 BQG's Programming Contest
    CodeForces 609D Gadgets for dollars and pounds
    CodeForces 609C Load Balancing
    CodeForces 609B The Best Gift
    CodeForces 609A USB Flash Drives
  • 原文地址:https://www.cnblogs.com/taoshihan/p/5625680.html
Copyright © 2011-2022 走看看