如何在android中的警报生成器中放入另一段代码?

时间:2014-11-16 08:15:48

标签: android

我正在使用警报构建器来获取用户在android中的确认,但我还需要将edittext放在警报构建器中以从用户那里获取一些数据。

3 个答案:

答案 0 :(得分:0)

您需要创建自己的自定义对话框:

http://www.mkyong.com/android/android-custom-dialog-example/

https://www.udemy.com/blog/android-alertdialog-examples/

或者做一些谷歌搜索“android自定义警报对话框”

答案 1 :(得分:0)

这是您如何完成任务的示例 专注于您问题的地点标有' - >':

    public void yourAlertDialog(int title, int message, int value, YourResultListener listener) {
                final View v;

                // inflate additional layout 
-->             v = LayoutInflater.from(this).inflate(R.layout.edit_text, null);

                // find desired views in the inflated layout:
-->             final EditText et = (EditText) v.findViewById(R.id.edit_text);
-->             et.setText("Your text - it may be passed as parameter");

                // generate AlertDialog and return result if OK is pressed
                new AlertDialog.Builder(this)//
                        .setTitle(title)//
                        .setMessage(message)//
                        .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener(){
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                if (listener != null) {
                                    //DO WHAT YOU NEED
                                }
                            }
                        })//
                        .setNegativeButton(android.R.string.cancel, null)//
-->                     .setView(v)// adding additional view
                        .show();
    }

请注意this这是您的主要活动

答案 2 :(得分:0)

最近我还需要在Alert Dialog中添加EditText,这里是代码:

AlertDialog.Builder alert = new AlertDialog.Builder(this);

alert.setTitle("Message");
alert.setMessage("Your custom message!");

// Set an EditText view to get user input
final EditText input = new EditText(this);
input.setText("Default text for EditText");
alert.setView(input);

alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
     public void onClick(DialogInterface dialog, int whichButton) {

               //Get value of EditText
               String value = input.getText().toString();

              //Do whatever you want to do with EditText value
     }
});

alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
     public void onClick(DialogInterface dialog, int whichButton) {
           // Canceled.
     }
});

alert.show();