Android AlertDialog SetText - PullRequest
       0

Android AlertDialog SetText

3 голосов
/ 29 апреля 2011

Привет, у меня есть alerttdialog, который создается, когда вы нажимаете на элемент в просмотре списка, я пытаюсь получить имя файла, описание, автора и т. Д. Из моей деятельности и отображать его в моем alerttdialog, но .setTextне работает, может кто-нибудь, пожалуйста, помогите.Спасибо, вот мой код: http://pastebin.com/FzWSPp5e

1 Ответ

1 голос
/ 29 апреля 2011

это совсем не то, как вы правильно используете диалоги в Android. вам нужно определить свои диалоги в переопределении onCreateDialog, как описано в документации:

http://developer.android.com/guide/topics/ui/dialogs.html

следуя этому руководству, вы сможете решить вашу проблему. Вот пример, который я только что скопировал и вставил из случайного приложения:

@Override
protected Dialog onCreateDialog(int id, Bundle b) {
    LayoutInflater inflater = (LayoutInflater) this.getSystemService(this.LAYOUT_INFLATER_SERVICE);
    AlertDialog.Builder builder = null;
    switch(id) {
        case DIALOG_BLOCK_SIZE:
        {
            Dialog dialog = new Dialog(this);
            final View dialogLayout = inflater.inflate(R.layout.dialog_block_size, null);
            builder = new AlertDialog.Builder(this);
            builder.setView(dialogLayout);
            builder.setTitle("Set Block Size");

            final EditText blockIn = (EditText)dialogLayout.findViewById(R.id.block_size_in);
            blockIn.setText(new Integer(pref.getInt("block_size", 6)).toString());

            builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        SharedPreferences.Editor editor = pref.edit();
                        editor.putInt("block_size", new Integer(blockIn.getText().toString()));
                        editor.commit();
                        ////////TODO///////////
                        //notify MinutemaidService that we have changed the block_size
                        dialog.dismiss();
                    }
                });
            builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        dialog.cancel();
                    }
                });
            dialog = builder.create();
            return dialog;
        }
        default:
        {
            return null;
        }
    }
    return dialog;
}

с указанным кодом, вы можете вызвать showDialog (DIALOG_BLOCK_SIZE), чтобы показать диалоговое окно. Также обратите внимание, что диалоги создаются один раз и показываются снова и снова. чтобы принудительно перестроить диалог, вызовите removeDialog (int) перед вызовом showDialog (int). Переопределение onPrepareDialog () - лучший метод, но использование removeDialog работает и это проще.

...