我有一个实现View.OnKeyListener
的Activity,以便当用户点击后退按钮并弹出对话框时它会捕获。以前,它工作得很好。然而,最近,它已停止工作,应用程序只是假装没有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;
}
}
答案 0 :(得分: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;
}
}
答案 1 :(得分:0)
尝试删除return true
下的builder.create().show();
,然后将return false
更改为return true
,看看它是否有效。这应该让他们在初始化KeyEvent时返回结果。
答案 2 :(得分:0)
最简单的方法是覆盖OnBackPressed并填写您想要在有人按下后退按钮时发生的代码。
确保删除super.OnBackPressed行。