Проблема с созданием AlertDialog - PullRequest
2 голосов
/ 11 августа 2011

Кажется, у меня проблема с созданием AlertDialog. Я пытаюсь создать собственный AlertDialog, который появляется при нажатии определенной кнопки RadioButton. Вот мой соответствующий код:

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        m_Minutes = -1;
        m_this=this;

        String prefName = getString(R.string.prefsfile);
        SharedPreferences settings = getSharedPreferences(prefName, 0);
        String defaultTextBackMessage = settings.getString("textBackMessage", getString(R.string.defaultTextBackMessage));
        EditText txtMessage = (EditText)findViewById(R.id.editText1);
        txtMessage.setText(defaultTextBackMessage);

        final Button button = (Button)findViewById(R.id.button1);
        final RadioButton manualButton = (RadioButton)findViewById(R.id.radio0);
        final RadioButton button15 = (RadioButton)findViewById(R.id.radio1);
        final RadioButton button30 = (RadioButton)findViewById(R.id.radio2);
        final RadioButton customButton = (RadioButton)findViewById(R.id.radio3);

        manualButton.setChecked(true);
        customButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Context c = v.getContext();
                LayoutInflater factory = LayoutInflater.from(v.getContext());
                final View minuteEntryView = factory.inflate(R.layout.customtime, null);
                AlertDialog ad = AlertDialog.Builder(c).create(); // this is the trouble line
            }
        });

Я получаю ошибку в строке AlertDialog ad = AlertDialog.Builder(c).create();. Я получаю ошибку The Method Builder(Context) is undefined for the type AlertDialog. Очевидно, однако, что Документы Google API имеют конструктор Builder. Что я делаю не так?

Ответы [ 2 ]

8 голосов
/ 11 августа 2011

ты не должен говорить это ...

AlertDialog ad = new AlertDialog.Builder(c).create();

вы забыли new ключевое слово .. Как вы можете ясно видеть, он говорит, что метод не найден, что означает, что вы вызываете его конструктор обычным способом, но так же, как вы вызываете метод.

0 голосов
/ 11 августа 2011

вам нужно переопределить метод onCreateDialog (int id) в вашей активности. Внутри этого метода вы должны создать свой объект Dialog, а из события onClick вашего RadioButton вы должны вызвать диалог, используя метод showDialog (id).

См. Ниже код:

@Override
protected Dialog onCreateDialog(int id) 
{
    // TODO Auto-generated method stub

    AlertDialog dialog = null;
    AlertDialog.Builder builder = null;

    builder = new AlertDialog.Builder(this);

    switch(id) 
    {
    case USERNAME_PASSWORD_EMPTY:

        builder.setMessage("Please Enter Username and Password.");
        builder.setCancelable(false);

        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() 
        {
            public void onClick(DialogInterface dialog, int id) 
            {
                //Do what you want to do when user clicks OK button of dialog
            }
        });

        dialog = builder.create();

    break;
    }

    return dialog;
}

Вы должны вызвать это диалоговое окно, используя метод showDialog (id), как показано ниже:

showDialog(USERNAME_PASSWORD_EMPTY);
...