Android получить текст EditText для AlertDialog - PullRequest
0 голосов
/ 04 ноября 2018

Я должен взять текст из AlertDialog.

Для AlertDialog я использую специальную раскладку: custom_alertdialog

Вместо этого для всего проекта используйте: another layout

Я пытался так:

View layout = getLayoutInflater().Inflate(R.layout.custom_alertdialog,null);

Или так:

View layout = View.inflate(SimpleVideoStream.this, R.layout.custom_alertdialog, null);

Но текст не берется ни в одном случае. Как я могу это сделать?

AlertDialog.Builder builder = new AlertDialog.Builder(SimpleVideoStream.this);
        builder.setTitle("Add Subtitle");
        builder.setCancelable(true);

        builder.setPositiveButton(
                "Ok",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        View inflatedView = getLayoutInflater().inflate(R.layout.custom_alertdialog, null);
                        final View layout = View.inflate(SimpleVideoStream.this, R.layout.custom_alertdialog, null);

                        EditText url_ele = (EditText) layout.findViewById(R.id.url);
                        String sub = url_ele.getText().toString();
                        Log.v("Ok:","/"+sub);
                        if (!sub.equals("")) {
                            showSub = !showSub;
                            Format textFormat = Format.createTextSampleFormat(null, MimeTypes.APPLICATION_SUBRIP,
                                    null, Format.NO_VALUE, Format.NO_VALUE, "en", null, Format.OFFSET_SAMPLE_RELATIVE);
                            MediaSource textMediaSource = new SingleSampleMediaSource.Factory(dataSourceFactory)
                                    .createMediaSource(Uri.parse(sub), textFormat, C.TIME_UNSET);

                            videoSource = new MergingMediaSource(videoSource, textMediaSource);
                            player.prepare(videoSource, false, false);
                        }
                        dialog.cancel();
                    }
                });

        builder.setNegativeButton(
                "Close",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        dialog.cancel();
                    }
                });

        AlertDialog alert = builder.create();
        LayoutInflater inflater = getLayoutInflater();
        View dialoglayout = inflater.inflate(R.layout.custom_alertdialog, null);
        alert.setView(dialoglayout);
        alert.show();

Ответы [ 2 ]

0 голосов
/ 04 ноября 2018

В вашем builder экземпляре вы раздули макет (макет A ). До использования метода setView() вы раздули другой макет (макет B ). Эти два макета отличаются.

Вы можете сделать так:

AlertDialog.Builder builder = new AlertDialog.Builder(SimpleVideoStream.this);
LayoutInflater inflater = this.getLayoutInflater();
View dialogView= inflater.inflate(R.layout.custom_alertdialog, null); 
builder.setView(inflater.inflate(R.layout.custom_alertdialog, null))
final EditText editText = (EditText)dialogView.findViewById(R.id.url); 

builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {
                     //Do something
                    }
                }
            });
       .setNegativeButton(){
                   //Do something
       };
builder.create();
builder.show();
0 голосов
/ 04 ноября 2018

Когда вы раздуваете View, он не делится экземпляром с другими вашими раздуваниями. То, что вы передали setView(), полностью отличается от того, что вы называете findViewById(). Вам нужно создать только один экземпляр, а затем сослаться на него.

Например:

View view1 = inflater.inflate(R.layout.some_layout, null);
View view2 = inflater.inflate(R.layout.some_layout, null);

EditText text1 = view1.findViewById(R.id.some_edittext);
EditText text2 = view2.findViewById(R.id.some_edittext);

text1.setText("Hello");

String text2text = text2.getText().toString(); //this will be null, because text1 and text2 aren't the same instance, because view1 and view2 aren't the same instance.

Перемещение

View dialoglayout = inflater.inflate(R.layout.custom_alertdialog, null);

выше, где вы устанавливаете положительную кнопку и делаете ее окончательной:

final View dialoglayout = inflater.inflate(R.layout.custom_alertdialog, null);
builder.setPositiveButton(...

Удалите inflatedView и layout изнутри вашего слушателя и используйте dialogLayout:

EditText url_ele = (EditText) dialogLayout.findViewById(R.id.url);
...