“警报”对话框中的按钮不起作用

时间:2012-01-19 02:20:20

标签: android android-activity dialog android-intent

好的,所以我最初正在努力将附件放在我的应用上,但首先我需要让我的对话框按钮工作。请帮助我真的不知道我需要做些什么才能使按钮正常工作。

Attachment.setOnClickListener(new View.OnClickListener() {
                public void onClick(View view) {

                    final CharSequence[] items = { "Picture", "Video", "Audio" };

                    AlertDialog.Builder builder = new AlertDialog.Builder(SMS.this);
                    // Read Update
                    builder.setTitle("Attachment");
                    builder.setIcon(R.drawable.ic_launcher);
                    builder.setItems(items, new DialogInterface.OnClickListener() {

                        public void onClick(DialogInterface dialog, int which) {
                            // TODO Auto-generated method stub
                            if (items.equals("Picture")) {
                                Intent nextScreen1 = new Intent(
                                        getApplicationContext(),
                                        FirstLoginActivity.class);
                                startActivity(nextScreen1);
                            }
                            if (items.equals("Video")) {
                                Intent nextScreen1 = new Intent(
                                        getApplicationContext(),
                                        FirstLoginActivity.class);
                                startActivity(nextScreen1);
                            }
                            if (items.equals("Audio")) {
                                Intent nextScreen1 = new Intent(
                                        getApplicationContext(),
                                        FirstLoginActivity.class);
                                startActivity(nextScreen1);
                            }
                        }

                    });

                    builder.create(); // <-- See This!
                    builder.show();
                }

            });
        }

我没有收到错误,但它没有做任何事情。

1 个答案:

答案 0 :(得分:2)

如何用此替换OnClick()

@Override
public void onClick(DialogInterface dialog, int which) {
    switch(which){
    case 0: // Picture
        Intent nextScreen1 = new Intent(
                getApplicationContext(),
                FirstLoginActivity.class);
        startActivity(nextScreen1);
        break;
    case 1: // Video
        Intent nextScreen1 = new Intent(
                getApplicationContext(),
                FirstLoginActivity.class);
        startActivity(nextScreen1);
        break;
    case 2: // Audio
        Intent nextScreen1 = new Intent(
                getApplicationContext(),
                FirstLoginActivity.class);
        startActivity(nextScreen1);
        break;
    }
}

实际上,如果你不想使用我上面的建议,你只需要纠正你的if statement

更改:(items.equals("Picture")) to (items[which].equals("Picture"))