Override back button to act like home button

后端 未结 10 831
小鲜肉
小鲜肉 2020-11-22 08:38

On pressing the back button, I\'d like my application to go into the stopped state, rather than the destroyed state.

In the Android docs it states:

10条回答
  •  星月不相逢
    2020-11-22 09:12

    I have use @Mirko N. answser using made the new Custom EditText

     public class EditViewCustom extends EditText {
    
        Button cancelBtn;
        RelativeLayout titleReleLayout;
        public EditViewCustom(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }
    
        public EditViewCustom(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public EditViewCustom(Context context) {
            super(context);
        }
    
        public void setViews(Button cancelBtn,RelativeLayout titleReleLayout){
            this.cancelBtn = cancelBtn;
            this.titleReleLayout = titleReleLayout;
        }
    
        @Override
        public boolean onKeyPreIme(int keyCode, KeyEvent event) {
            if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
                Log.d("KEYCODE_BACK","KEYCODE_BACK");
                cancelBtn.setVisibility(View.GONE);
                this.setFocusableInTouchMode(false);
                this.setFocusable(false);
                titleReleLayout.setVisibility(View.VISIBLE);
    
                return super.onKeyPreIme(keyCode, event);
              }
    
            return super.onKeyPreIme(keyCode, event);
        }
    
    }
    

    Then set data from your activity

     searchEditView.setViews(cancelBtn, titleRelativeLayout);
    

    Thank you.

提交回复
热议问题