Android将文本EditText获取到AlertDialog

时间:2018-11-03 23:29:19

标签: java android android-layout alertdialog

我必须从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 :(得分:1)

为视图充气时,它与其他任何充气实例都不共享实例。因此,您传递给_list = str(_list) 的内容与您调用_list.append()的内容完全不同。您只需要创建一个实例,然后引用它即可。

例如:

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

答案 1 :(得分:1)

在您的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();