如果布尔值为真,则覆盖主页和后退按钮 [英] Override home and back button is case a boolean is true

查看:18
本文介绍了如果布尔值为真,则覆盖主页和后退按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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
   }

推荐答案

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

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

是的,您可以覆盖 Home 按钮.

Yes you can do override Home button.

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

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

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>

这篇关于如果布尔值为真,则覆盖主页和后退按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆