java - Android获取文本EditText到AlertDialog

标签 java android android-layout android-alertdialog

我必须从 AlertDialog 中获取文本。

对于 AlertDialog,我使用特定的布局:custom_alertdialog

对于所有项目使用:另一个布局

我尝试过这样的:

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();

最佳答案

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

关于java - Android获取文本EditText到AlertDialog,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53136428/

相关文章:

java - 下面的片段是什么意思?

android - 设计屏幕在 Android studio 2.2 中消失

java - 在转换为 Java 中的字符串之前,我应该如何终止一个 char 数组

concurrency - 分析 OpenJDK 或任何 Java VM 中的锁

java - 按下后退按钮时不加载新首选项

android - 剪辑按钮不接收触摸输入

android - 以编程方式将 View 添加到 RelativeLayout android

android - 使用 Firebase 跟踪应用内结算

android - 在 Android 中使用 5Mill 条目占用 0.5GB 的 SQLite 数据库是否可行?

android - "GONE" View 是否膨胀?