1

我有一个实现的活动,View.OnKeyListener以便它可以捕捉用户点击后退按钮并弹出一个对话框。以前,它工作得很好。然而,最近它已经停止工作,并且该应用程序只会假装没有onKey方法可以捕捉“后退”按钮。我似乎无法弄清楚为什么,因为onKey自从我做出改变以来我几乎没有接触过这个方法。我尝试只使用OnBackPressed(),但这并没有解决任何问题,实际上产生了奇怪和不可预测的行为。谁能帮我弄清楚为什么我的应用程序似乎让系统在后退按钮上占据主导地位?

这是我实现的重要部分:

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.actionbarsherlock.view.MenuInflater;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.KeyEvent;
import android.view.View;

public class NagTasksAddTasksActivity extends SherlockFragmentActivity implements View.OnKeyListener {
    //various parameters
    public void onCreate(Bundle savedInstanceBundle)
    {
        super.onCreate(savedInstanceBundle);
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
        String setTheme = prefs.getString("themeselect", "Dark Overflow");
        if (setTheme.toLowerCase().contentEquals("dark overflow")){
            setTheme(R.style.Theme_Sherlock_ForceOverflow);
        } else if (setTheme.toLowerCase().contentEquals("light overflow")){
            setTheme(R.style.Theme_Sherlock_Light_ForceOverflow);
            isDarkThemeUsed = false;
        } else if (setTheme.toLowerCase().contentEquals("dark menu")){
            setTheme(R.style.Theme_Sherlock);
        } else if (setTheme.toLowerCase().contentEquals("light menu")){
            setTheme(R.style.Theme_Sherlock_Light);
            isDarkThemeUsed = false;
        }
        setContentView(R.layout.createtasklayout);
        ActionBar actionBar = getSupportActionBar();
        setTitle(R.string.newTask);

    }
    ...
    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK)
        {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setCancelable(true);
            builder.setTitle(R.string.wait);
            builder.setMessage(R.string.confirmbackkey);
            builder.setPositiveButton(R.string.save, new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    //stuff
                    dialog.dismiss();
                }
            });
            builder.setNegativeButton(R.string.discard, new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    AlertDialog.Builder subBuilder = new AlertDialog.Builder(getParent());
                    subBuilder.setCancelable(true);
                    subBuilder.setTitle(R.string.wait);
                    subBuilder.setMessage(R.string.confirmdiscard);
                    subBuilder.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            //more stuff
                            dialog.dismiss();
                        }
                    });
                    subBuilder.setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.cancel();
                        }
                    });
                    dialog.dismiss();
                    subBuilder.create().show();
                }
            });
            builder.setNeutralButton(R.string.cancel, new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });
            builder.create().show();
            return true;
        }
        return false;
    }
}
4

3 回答 3

4

另一种捕捉返回按钮的方法是在Activity中使用dispatchKeyEvent方法(不需要实现View.OnKeyListener)。这似乎每次都对我有用。

public class HomeActivity extends Activity
{
...

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) 
    {
        int action = event.getAction();
        int keyCode = event.getKeyCode();
        switch (keyCode) 
        {
            case KeyEvent.KEYCODE_BACK:
                if (action == KeyEvent.ACTION_DOWN)
                {
                    Log.v("myApp", "Back button pressed.");
                    return true;
                }
            default:
                return false;
        }
        return false;
    }
}
于 2012-06-27T01:08:54.523 回答
0

尝试移除return true下面builder.create().show();并更改return falsereturn true,看看它是否有效。这应该让它们在初始化 KeyEvent 时都返回结果。

于 2012-06-26T22:40:53.067 回答
0

最简单的方法是覆盖 OnBackPressed 并填写您希望在有人按下后退按钮时发生的代码。

只需确保删除 super.OnBackPressed 行。

于 2012-06-26T23:56:39.073 回答