简体   繁体   English

如何不关闭AlertDialog Android

[英]How to not close AlertDialog android

Hi my problem is when I select an item my AlertDialog dismiss 嗨,我的问题是当我选择一个项目时, AlertDialog关闭了

alertDialog = new AlertDialog.Builder(getActivity());
alertDialog
        .setSingleChoiceItems(ageArr, 1, btnSelectItem)
        .setPositiveButton(R.string.dialog_ok, btnPositiveAgeDialog)
        .setNegativeButton(R.string.dialog_cancel, null)
        .show();

what my dialog click positive looks is. 我的对话框点击正面看起来是什么。

private DialogInterface.OnClickListener btnSelectItem = new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        selectedIndexAge = which;
    }
};

I tried setting the listener to null and it does not close but still I needed it because I wanted to know which item is selected 我尝试将侦听器设置为null,但它不会关闭,但是我仍然需要它,因为我想知道选择了哪个项目

Just put it 放一下

itemView.setOnClickListener(null);

or 要么

You can use the implementation of hasOnClickListeners() for knowing the status of listener taken from android.view.View class for 您可以使用hasOnClickListeners()的实现来了解从android.view.View类获取的侦听器的状态

 public boolean hasOnClickListeners() {
        ListenerInfo li = mListenerInfo;
        return (li == null && li.mOnClickListener == null);
    }

Use the following link for further modifications 使用以下链接进行进一步修改

Set listener instance in fragment on application restore 在应用程序还原时在片段中设置侦听器实例

try this 尝试这个

 dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener()
  {            
      @Override
      public void onClick(View v)
      {
          Boolean wantToCloseDialog = false;
          //Do stuff, possibly set wantToCloseDialog to true then...
          if(wantToCloseDialog)
              dialog.dismiss();
          //else dialog stays open. Make sure you have an obvious way to close the dialog especially if you set cancellable to false.
      }
  });

Something else must be closing your AlertDialog . 某些其他原因可能正在关闭AlertDialog Below is a program that I believe duplicates the minimum requirements you have posted, and selecting one of the items does not close the dialog. 我相信以下程序可以重复您发布的最低要求,并且选择其中一项不会关闭对话框。

public class MainActivity extends AppCompatActivity {

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

        String[] values = new String[]{ "one", "two", "three", "four" };

        DialogInterface.OnClickListener choiceListener = new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "selected index: " + which, Toast.LENGTH_SHORT).show();
            }
        };

        DialogInterface.OnClickListener positiveListener = new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "positive button", Toast.LENGTH_SHORT).show();
            }
        };

        new AlertDialog.Builder(this)
                .setSingleChoiceItems(values, 1, choiceListener)
                .setPositiveButton("ok", positiveListener)
                .setNegativeButton("cancel", null)
                .show();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM