Mi chiedevo se è possibile ignorare l'azione del pulsante Indietro e Home in alcuni casi.
Sì, è possibile eseguire l'override del pulsante Home
.
Ho sviluppato un'applicazione che disabilita il pulsante duro, puoi dare un'occhiata. io ho preso un pulsante di commutazione che blocca tutto il pulsante difficile lavorare tranne Potenza pulsante
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>
fonte
2012-04-05 09:18:38
Wow, questo è grande, esattamente ciò che sto cercando ! – Diego
Non lo sapevo, pensavo che non potessi cambiarlo, +1 :) – Bigflow
Ottimo! il suo funzionamento perfectoo. – Dharmendra