1

应用程序启动正常,但是当我单击 Select_Players 按钮时,我的设备上没有出现对话框。这是代码:

public class MainActivity extends Activity {


    private Button selectPlayers;

            @Override
        protected void onCreate(final Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_demo);

            super.onStart();    //customize
            super.onResume();   //customize

            selectPlayers = (Button) findViewById(R.id.add_players);

            selectPlayers.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View view) {

                    // Launch dialogbox on click
                    onCreateDialog(savedInstanceState);

                }
            });
        }

        public Dialog onCreateDialog(Bundle savedInstanceState) {

            @SuppressWarnings("rawtypes")
            final ArrayList mSelectedItems = new ArrayList();  // Where we track the selected items
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            // Set the dialog title
            builder.setTitle(R.string.select_players)

            // Specify the list array, the items to be selected by default (null for none),
            // and the listener through which to receive callbacks when items are selected
            .setMultiChoiceItems(R.array.players_name, null,
                    new DialogInterface.OnMultiChoiceClickListener() {

                @SuppressWarnings("unchecked")
                @Override
                public void onClick(DialogInterface dialog, int which,
                        boolean isChecked) {
                    if (isChecked) {
                        // If the user checked the item, add it to the selected items
                        mSelectedItems.add(which);
                    } else if (mSelectedItems.contains(which)) {
                        // Else, if the item is already in the array, remove it 
                        mSelectedItems.remove(Integer.valueOf(which));
                    }
                }
            })

            // Set the action buttons
            .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int id) {

                    //CODE TO CLOSE DIALOGBOX AND START FORGE

                }
            })
            .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int id) {

                    //CODE TO JUST CLOSE DIALOGBOX

                }
            });

            return builder.create();
        }
}

我看到 Dialog 方法返回一个 Dialog 但我不知道如何使它显示为 onClick 的结果?(作为参考,我从 Android 开发网站获取了 Dialog 方法。)

谢谢!

4

4 回答 4

1

此方法返回一个对话框,因此您必须在您的 onClick 中构建一个这样的对话框

     Dialog d = onCreateDialog(savedInstanceState);
     d.show();

我认为您尝试的是覆盖 Activity 方法 onCreateDialog(),但您必须以另一种方式进行,如下所示:

http://www.mysamplecode.com/2011/11/android-alertdialog-example-showdialog.html

于 2013-07-31T13:22:06.613 回答
0

First of all set the listener to the button in the onCreate().

private Button selectPlayers;
    @Override
protected void onCreate(final Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_demo);

    super.onStart();    //customize
    super.onResume();   //customize

    selectPlayers = (Button) findViewById(R.id.add_players);
    //set the listener to the button
    selectPlayers.setOnClickListener(this)
}

Your code for your dialog box.

    public Dialog onCreateDialog(Bundle savedInstanceState) {
    @SuppressWarnings("rawtypes")
    final ArrayList mSelectedItems = new ArrayList();  // Where we track the selected items
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    // Set the dialog title
    builder.setTitle(R.string.select_players)

    // Specify the list array, the items to be selected by default (null for none),
    // and the listener through which to receive callbacks when items are selected
    .setMultiChoiceItems(R.array.players_name, null,
            new DialogInterface.OnMultiChoiceClickListener() {

        @SuppressWarnings("unchecked")
        @Override
        public void onClick(DialogInterface dialog, int which,
                boolean isChecked) {
            if (isChecked) {
                // If the user checked the item, add it to the selected items
                mSelectedItems.add(which);
            } else if (mSelectedItems.contains(which)) {
                // Else, if the item is already in the array, remove it 
                mSelectedItems.remove(Integer.valueOf(which));
            }
        }
    })

    // Set the action buttons
    .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int id) {

            //CODE TO CLOSE DIALOGBOX AND START FORGE

        }
    })
    .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int id) {

            //CODE TO JUST CLOSE DIALOGBOX

        }
    });
    AlertDialog alert = builder.create();
    alert.show(); 

}

Having one onClick() method means you can handle many buttons etc with one method, using an if/case statement to differentiate each one, here if the arg0 is the button that is used to call the dialog then it should call the method that handles your dialog box.

public void onClick(View arg0)
{  
        if(arg0== selectPlayers){
         //Show your dialog
 }
}

This is personal preference doing it this way, I feel it keeps everything tidy and together but again its just my opinion.

于 2013-07-31T13:38:58.727 回答
0

您缺少应该使用的 show() 函数,如下所示...

对话框.show();

于 2013-07-31T13:31:51.843 回答
0

尝试

selectPlayers.setOnClickListener(new Button.OnClickListener() {

            @Override
            public void onClick(View view) {

                // Launch dialogbox on click
                onCreateDialog(savedInstanceState);

            }
        });

始终使用 View Extending 对象内的 Listeners。我将您的代码“视图”更改为“按钮”仅此而已。我还没有测试过它,我记得经常像你一样做它并且它有效。但我记得最近几天的一个问题取决于 android API 在 Week Hashmaps 中处理内联侦听器的方式,这些侦听器会收集垃圾。如果这是您的问题,解决方案非常简单。将您的侦听器附加到一个变量并传递该变量。

例子:

Button.OnClickListener listener = new View.OnClickListener() {

            @Override
            public void onClick(View view) {

                // Launch dialogbox on click
                onCreateDialog(savedInstanceState);

            }
        }; 
selectPlayers.setOnClickListener(listener)

这样,您的 Activity 将持有侦听器并将其实例传递给按钮。现在垃圾收集器不应该删除它,直到您的活动被删除或您手动删除它。

哦..还没有看到您对“onCreateDialog”的调用。第一个以“on”开头的方法是生命周期方法,它将从系统调用,而不是手动调用。然后我认为不推荐使用 onCreateDialog 。对于这个问题,我参考 Opiatefuchs 的答案。

于 2013-07-31T13:22:52.087 回答