在所有活动中使用 public boolean onKeyDown

Use public boolean onKeyDown in all activities

我是 Android Studio 的新手,我创建了一个包含 10 个活动的简单应用程序。

现在,我想在我的所有活动中拦截来自用户的 KeyEvent.KEYCODE_BACK。

我在 KEYCODE_BACK 时打开一个对话框。在我的 MainActivity 中没问题,但我不想在我的所有活动中复制此代码。

有人有想法吗?

public class MainActivity extends AppCompatActivity
    {
        Dialog myDialog;
        Button BoutonAccepter;
        Button BoutonRefuser;

        @Override
        protected void onCreate(Bundle savedInstanceState)
            {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
            }

        @Override
        public boolean onKeyDown(int keyCode, KeyEvent event)
            {
                myDialog = new Dialog(this);
                if (keyCode == KeyEvent.KEYCODE_BACK)
                    {
                        myDialog.setContentView(R.layout.popup);
                        BoutonAccepter = (Button) myDialog.findViewById(R.id.BoutonAccepter);
                        BoutonRefuser = (Button) myDialog.findViewById(R.id.BoutonRefuser);

                        BoutonAccepter.setOnClickListener(new View.OnClickListener()
                            {
                                @Override
                                public void onClick(View v)
                                {
                                    Toast.makeText(getApplicationContext(), "Non", Toast.LENGTH_LONG).show();
                                    myDialog.dismiss();
                                }
                            });

                        BoutonRefuser.setOnClickListener(new View.OnClickListener()
                            {
                                @Override
                                public void onClick(View v)
                                {
                                    Toast.makeText(getApplicationContext(), "Oui", Toast.LENGTH_LONG).show();
                                    myDialog.dismiss();
                                }
                            });
                        myDialog.show();
                    }
                else
                    {
                        return super.onKeyDown(keyCode, event);
                    }
                return true;
            }
    }

我认为您应该将此代码分成一个单独的摘要 class 并扩展此 class 用于您的所有活动

public abstract class BaseActivity extends AppCompatActivity
    {
        Dialog myDialog;
        Button BoutonAccepter;
        Button BoutonRefuser;

        @Override
        public boolean onKeyDown(int keyCode, KeyEvent event)
            {
                myDialog = new Dialog(this);
                if (keyCode == KeyEvent.KEYCODE_BACK)
                    {
                        myDialog.setContentView(R.layout.popup);
                        BoutonAccepter = (Button) myDialog.findViewById(R.id.BoutonAccepter);
                        BoutonRefuser = (Button) myDialog.findViewById(R.id.BoutonRefuser);

                        BoutonAccepter.setOnClickListener(new View.OnClickListener()
                            {
                                @Override
                                public void onClick(View v)
                                {
                                    Toast.makeText(getApplicationContext(), "Non", Toast.LENGTH_LONG).show();
                                    myDialog.dismiss();
                                }
                            });

                        BoutonRefuser.setOnClickListener(new View.OnClickListener()
                            {
                                @Override
                                public void onClick(View v)
                                {
                                    Toast.makeText(getApplicationContext(), "Oui", Toast.LENGTH_LONG).show();
                                    myDialog.dismiss();
                                }
                            });
                        myDialog.show();
                    }
                else
                    {
                        return super.onKeyDown(keyCode, event);
                    }
                return true;
            }
    }

并且在所有活动中

public class ActivityA extends BaseActivity {
}