Android 如果布尔值为真,则覆盖主页和后退按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10025660/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 02:14:24  来源:igfitidea点击:

Override home and back button is case a boolean is true

androidbuttonoverridingback-button

提问by Diego

I was wondering if I can override the action of the back and home button is some cases. Normally these buttons should just react like they always do, but in a case some setting is true I want to override the buttons and let them call my own methods.

我想知道在某些情况下是否可以覆盖后退和主页按钮的操作。通常这些按钮应该像往常一样做出反应,但在某些设置为真的情况下,我想覆盖这些按钮并让它们调用我自己的方法。

I′m using these two methods to override these buttons:

我正在使用这两种方法来覆盖这些按钮:

  @Override
  public void onBackPressed() {    
  // call my backbutton pressed method when boolean==true
    }

  @Override
  public void onAttachedToWindow()    {                                                                                       this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);    
   super.onAttachedToWindow(); 
   // call my homebutton pressed method when boolean==true
   }

回答by Mohammed Azharuddin Shaikh

I was wondering if I can override the action of the back and home button is some cases.

我想知道在某些情况下是否可以覆盖后退和主页按钮的操作。

Yes you can do override Homebutton.

是的,您可以执行覆盖Home按钮。

I have developed an application which disable hard button, you can have a look. I have taken a toggle buttonwhich locks all hard button to work except Powerbutton

我开发了一个禁用硬按钮的应用程序,你可以看看。我使用了一个切换按钮,它可以锁定除电源按钮之外的所有硬按钮工作

public class DisableHardButton extends Activity {
    /** Called when the activity is first created. */
    TextView mTextView;
    ToggleButton mToggleButton;
    boolean isLock=false;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mTextView=(TextView) findViewById(R.id.tvInfo);
        mToggleButton=(ToggleButton) findViewById(R.id.btnLock);


        mToggleButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            // TODO Auto-generated method stub
            isLock=isChecked;
            onAttachedToWindow();
        }
    });
   }
@Override
    public boolean dispatchKeyEvent(KeyEvent event) {

        if ( (event.getKeyCode() == KeyEvent.KEYCODE_HOME) && isLock) {
            mTextView.setText("KEYCODE_HOME");
            return true;
        }
        else
            return super.dispatchKeyEvent(event);
    }
@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub

        if( (keyCode==KeyEvent.KEYCODE_BACK) && isLock)
        {
            mTextView.setText("KEYCODE_BACK");
            return true;
        }
           else
             return super.onKeyDown(keyCode, event);
    }
@Override
    public void onAttachedToWindow()
    {  
        System.out.println("Onactivity attached :"+isLock);
        if(isLock)
        {   
            this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);     
            super.onAttachedToWindow();
        }
        else
        {
            this.getWindow().setType(WindowManager.LayoutParams.TYPE_APPLICATION);     
            super.onAttachedToWindow();
        }
    }
}

main.xml

主文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/tvInfo"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <ToggleButton
        android:id="@+id/btnLock"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textOff="UnLocked"
        android:textOn="Locked" />

</LinearLayout>

回答by NitroG42

You call super.onBackPressed() to call the normal method. Exemple :

您调用 super.onBackPressed() 来调用普通方法。例子:

@Override
public void onBackPressed() {    
    if (activated) {
       //doyourthing
    } else {
       super.onBackPressed()
    }
}

回答by Blackbelt

No you can not. What you can do is to ovveride the method and manage the boolean inside it:

你不能。您可以做的是覆盖该方法并管理其中的布尔值:

for instance:

例如:

 public void onBackPressed() {    // call my backbutton pressed method when boolean==true

      if (myCondition) {
          // take care of my needs
       } else 
            // call super to let the back behavior be "normal"

  }

回答by Adil Soomro

Regarding overriding the behaviour of Home Button you are out of luck.

关于覆盖主页按钮的行为,您很不走运

However if your app is a specific one and have limited targeted audience, like inter-organization app, hospital kiosk, restaurant ordering, you can try making your app as Home (the launcher app). You can find a good example here: How to Write Custom Launcher App in Android

但是,如果您的应用程序是特定的并且目标受众有限,例如跨组织应用程序、医院信息亭、餐厅订购,您可以尝试将您的应用程序设为 Home(启动器应用程序)。你可以在这里找到一个很好的例子:How to Write Custom Launcher App in Android

And to override the back key events, lot of examples are there.

为了覆盖后退键事件,有很多例子。

For example:

例如:

  1. Catch keypress with android
  2. Override back button to act like home button
  3. Android - How To Override the "Back" button so it doesn't Finish() my Activity?
  1. 用android捕捉按键
  2. 覆盖后退按钮以充当主页按钮
  3. Android - 如何覆盖“后退”按钮使其不完成()我的活动?

回答by vipin

make your boolean variable member variable

使您的布尔变量成为成员变量

boolean temp;

@Override
        public void onBackPressed() {    // call my backbutton pressed method when boolean==true

if(temp)
//your methode
else
finish();
            }

回答by Bigflow

I use this:

我用这个:

public void onBackPressed() {

        switch (screen) {
        case 1:
            screen = 99;
            setContentView(R.layout.menu);
            break;

        case 99:
            finish();
            break;

        }
        return;
    }

When I am in a other screen (other then menu screen), I set the variable screen to 1. When I press the back button, it goes back to the menu screen (instead of killing the app). and give the screen variable the number 99, then when you hit the back button again, it kills the app.

当我在另一个屏幕(菜单屏幕之外)时,我将变量屏幕设置为 1。当我按下后退按钮时,它会返回到菜单屏幕(而不是终止应用程序)。并为屏幕变量指定数字 99,然后当您再次点击后退按钮时,它会杀死应用程序。

However, you can't change the home button.

但是,您无法更改主页按钮。