Android setOnClick不工作/ Dialogbox没有出现

时间:2013-07-31 13:16:29

标签: android onclick dialogfragment

应用程序启动正常但当我单击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 个答案:

答案 0 :(得分:1)

此方法返回一个对话框,因此您必须在OnClick

中构建一个这样的对话框
     Dialog d = onCreateDialog(savedInstanceState);
     d.show();

我认为你所尝试的是覆盖Activity方法onCreateDialog(),但你必须以antoher方式执行,如下所示:

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

答案 1 :(得分:0)

尝试

selectPlayers.setOnClickListener(new Button.OnClickListener() {

            @Override
            public void onClick(View view) {

                // Launch dialogbox on click
                onCreateDialog(savedInstanceState);

            }
        });

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

示例:

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

            @Override
            public void onClick(View view) {

                // Launch dialogbox on click
                onCreateDialog(savedInstanceState);

            }
        }; 
selectPlayers.setOnClickListener(listener)

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

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

答案 2 :(得分:0)

你缺少show()函数,你应该使用如下......

dialog.show();

答案 3 :(得分:0)

首先将监听器设置为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)
}

对话框的代码。

    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(); 

}

有一个onClick()方法意味着您可以使用一个方法处理许多按钮等,使用if / case语句来区分每个按钮,如果arg0是用于调用对话框的按钮,那么它应该调用处理对话框的方法。

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

这是个人偏好这样做,我觉得它保持一切整洁,但再一次只是我的意见。