简体   繁体   English

Android中带有editText的AlertDialog

[英]AlertDialog with an editText in Android

I need to put in an editText inside of an alertDialog. 我需要在alertDialog中放入editText。 From what I can understand, from this , I need to create a custom dialog. 从我可以理解,从这个 ,我需要创建一个自定义对话框。 Is that really the only way to put an editText in a alertDialog? 那真的是将editText放入alertDialog的唯一方法吗? I don't want to take make steps than needed to complete a simple task. 我不想采取多余的步骤来完成一个简单的任务。 It should like this in the end: 最后应该这样:

------------------------
-Please enter your name-
- [                   ]-
-         (Save)       -
------------------------
AlertDialog.Builder alert = new AlertDialog.Builder(this);

alert.setTitle("Title");
alert.setMessage("Message");

// Set an EditText view to get user input 
final EditText input = new EditText(this);
alert.setView(input);

This way you don't have to create a new custom dialogue. 这样,您不必创建新的自定义对话框。

Yes, this is the proper (and potentially only) way to do this at present. 是的,这是目前执行此操作的正确(且可能仅)方法。

Something like this: 像这样:

final EditText input = new EditText(this);
AlertDialog.Builder alert = new AlertDialog.Builder(this)
    .setTitle("Please enter your name")
    .setView(input) // Use our EditText
    .setPositiveButton("Save", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            // Save value of `input.getText()`
        }
    }).show();

This is a customized version of the code from this blog that I use from time to time. 这是我不时使用的来自此博客的代码的自定义版本。

You can not only put EditText, but any thing you want. 您不仅可以放置EditText,还可以放置任何您想要的东西。 Just create an XML layout for what ever you want to put and then use it as is shown in the link :- 只需为您想要放置的内容创建XML布局,然后按照链接中所示使用它即可:-

how to get customized alert dialog , like the one shown in image? 如何获得自定义的警报对话框,如图所示?

Refer the answer which I've given (Aamir Shah) 请参阅我给出的答案(阿米尔·沙阿)

Hope the explanation was useful... 希望解释是有用的...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM