Android AlertDialog 在菜单选项中调用时不会显示
当调用菜单中的某个选项时,我试图调用 AlertDialog。用户选择的内容对于作为参数传递到网络上的 XML 页面非常重要。
我已尝试以下操作,但我的 AlertDialog 从未显示,它直接进入我的下一个活动。
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.kantoor:
//Opent de office activity
final SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
String arbeiderbed = pref.getString("ArbeiderBediende", "");
if(arbeiderbed.equals("")){
final CharSequence[] beroep = {"Arbeider","Bediende"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Kies uw statuut");
builder.setSingleChoiceItems(beroep, -1, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
if (beroep[which].equals("Arbeider")){
SharedPreferences.Editor editor = pref.edit();
editor.putString("ArbeiderBediende", "arbeider");
editor.commit(); // Very important
}
else{
SharedPreferences.Editor editor = pref.edit();
editor.putString("ArbeiderBediende", "bediende");
editor.commit(); // Very important
}
}
});
builder.create().show();
}
Intent office = new Intent(MainActivity.this, Settings.class);
startActivity(office);
return true;
I'm trying to call an AlertDialog when a certain option in my menu is called. What the user chooses is important to pass as a parameter to an XML page on the net.
I've tried the following, but my AlertDialog never shows, it goes directly to my next Activity.
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.kantoor:
//Opent de office activity
final SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
String arbeiderbed = pref.getString("ArbeiderBediende", "");
if(arbeiderbed.equals("")){
final CharSequence[] beroep = {"Arbeider","Bediende"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Kies uw statuut");
builder.setSingleChoiceItems(beroep, -1, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
if (beroep[which].equals("Arbeider")){
SharedPreferences.Editor editor = pref.edit();
editor.putString("ArbeiderBediende", "arbeider");
editor.commit(); // Very important
}
else{
SharedPreferences.Editor editor = pref.edit();
editor.putString("ArbeiderBediende", "bediende");
editor.commit(); // Very important
}
}
});
builder.create().show();
}
Intent office = new Intent(MainActivity.this, Settings.class);
startActivity(office);
return true;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当然可以。警报对话框不是模态的,这意味着
show()
方法不会阻止进一步代码的执行。例如,如果您希望活动在对话框关闭时启动,则应将对startActivity()
的调用放入对话框按钮的单击侦听器中。Of course it does. Alert dialogs are not modal, meaning that the
show()
method does not block the execution of the further code. If you want the activity to start when the dialog closes, you should put the call tostartActivity()
, for instance, into the click listener of the dialog button.