Java Android get text EditText to AlertDialog
我必须从AlertDialog中获取文本 对于AlertDialog,我使用特定的布局:Java Android get text EditText to AlertDialog,java,android,android-layout,android-alertdialog,Java,Android,Android Layout,Android Alertdialog,我必须从AlertDialog中获取文本 对于AlertDialog,我使用特定的布局:custom\u AlertDialog 对于所有项目,请使用:另一个布局 我试着这样做: View layout = getLayoutInflater().Inflate(R.layout.custom_alertdialog,null); AlertDialog.Builder builder = new AlertDialog.Builder(SimpleVideoStream.this); Lay
custom\u AlertDialog
对于所有项目,请使用:另一个布局
我试着这样做:
View layout = getLayoutInflater().Inflate(R.layout.custom_alertdialog,null);
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();
大约:
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();
膨胀视图时,它不会与膨胀视图的任何其他时间共享实例。因此,您传递给
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.
移动
在上面设置肯定按钮并使其成为最终按钮:
final View dialoglayout = inflater.inflate(R.layout.custom_alertdialog, null);
builder.setPositiveButton(...
从监听器内部删除充气视图
和布局
,并使用对话框布局
:
EditText url_ele = (EditText) dialogLayout.findViewById(R.id.url);
膨胀视图时,它不会与膨胀视图的任何其他时间共享实例。因此,您传递给
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.
移动
在上面设置肯定按钮并使其成为最终按钮:
final View dialoglayout = inflater.inflate(R.layout.custom_alertdialog, null);
builder.setPositiveButton(...
从监听器内部删除充气视图
和布局
,并使用对话框布局
:
EditText url_ele = (EditText) dialogLayout.findViewById(R.id.url);
在
builder
实例中,您膨胀了一个布局(布局a)。在setView()
方法之前,您膨胀了另一个布局(布局B)。这两种布局是不同的
你可以这样做:
View layout = getLayoutInflater().Inflate(R.layout.custom_alertdialog,null);
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();
在
builder
实例中,您膨胀了一个布局(布局a)。在setView()
方法之前,您膨胀了另一个布局(布局B)。这两种布局是不同的
你可以这样做:
View layout = getLayoutInflater().Inflate(R.layout.custom_alertdialog,null);
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();
我这样做了:final View inflatedView=getLayoutInflater().inflate(R.layout.custom\u alertdialog,null);builder.setPositiveButton(…..但它似乎不起作用。不要照搬我放的东西。我只是通过显示应该放在下面的部分来告诉你该放在哪里。在没有阅读的情况下永远不要复制和粘贴代码!我这样做了:final View inflatedView=getLayoutInflater()。inflate(R.layout.custom_alertdialog,null);builder.setPositiveButton(……但它似乎不起作用。不要照抄我写的。我只是通过在下面显示部分内容来告诉你该放在哪里。不要在不阅读的情况下复制和粘贴代码!