0

我使用这段代码来构建一个带有 EditText 的 AlertDialog:

AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Title");
builder.setView(LayoutInflater.from(context).inflate(R.layout.dialog_view, null));
builder.setNegativeButton("Cancel", null);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        mInput = ((EditText) LayoutInflater.from(context).inflate(R.layout.dialog_view, null).findViewById(R.id.etxtDialog)).getText().toString();
    }
});
builder.show();

但是,当我运行此代码时,mInput.length() == 0,因此字符串为空。虽然该行mInput = ((EditText) LayoutInflater.from(context).inflate(R.layout.dialog_view, null).findViewById(R.id.etxtDialog)).getText().toString();已执行,但 EditText 确实包含一些字符。为什么这段代码不起作用?

4

5 回答 5

8

你做错了......拿着你的膨胀视图的实例,以后再使用它。例如:

AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Title");
final View v = LayoutInflater.from(context).inflate(R.layout.dialog_view, null);
builder.setView(v);
builder.setNegativeButton("Cancel", null);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
       mInput = ((EditText)v.findViewById(R.id.etxtDialog)).getText().toString();
    }
});
builder.show();
于 2013-04-30T11:15:10.700 回答
1

用这种方式

    ContextThemeWrapper cw = new ContextThemeWrapper( this, R.style.AlertDialogTheme );
    AlertDialog.Builder builder= new AlertDialog.Builder( cw );
    LayoutInflater inflater = (LayoutInflater) cw.getSystemService(LAYOUT_INFLATER_SERVICE);
    View layout = inflater.inflate(R.layout.dialog_view,null);    

    AlertDialog.Builder builder = new AlertDialog.Builder(context);
    builder.setTitle("Title");
    builder.setView(layout);
    mInput = (EditText)layout.findViewById(R.id.etxtDialog);
    builder.setNegativeButton("Cancel", null);
    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
           String value =  mInput.getText().toString();
        }
    });
    builder.show();

这里: R.style.AlertDialogTheme 是你的应用程序主题

于 2013-04-30T11:16:01.903 回答
1

这是因为您每次单击positiveButtoninflate每次都调用)时都会创建一个新视图。你应该这样做:

AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Title");
View v=LayoutInflater.from(context).inflate(R.layout.dialog_view, null);
builder.setView(v);
builder.setNegativeButton("Cancel", null);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        mInput = ((EditText) v.findViewById(R.id.etxtDialog)).getText().toString();
    }
});
builder.show();
于 2013-04-30T11:16:38.493 回答
0

尝试这个。您的完整和最终解决方案。我就是这样做的:

LayoutInflater li = LayoutInflater.from(this);
    View promptsView = li.inflate(R.layout.prompts, null);

    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
    alertDialogBuilder.setView(promptsView);

    final EditText userInput = (EditText) promptsView.findViewById(R.id.editTextDialogUserInput);

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy_MM_dd_HH.mm.ss");
    String currentDateandTime = sdf.format(new Date());
    userInput.setText(currentDateandTime);

    alertDialogBuilder.setCancelable(false);

    alertDialogBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener()
                                                    {
                                                        public void onClick(DialogInterface dialog,int id)
                                                        {
                                                            if(userInput.getText()+"" != "")
                                                            {
                                                                Intent i = new Intent(MainActivity.this,AskForTextFile.class);
                                                                i.putExtra("userInput",userInput.getText()+"");
                                                                startActivity(i);
                                                            }
                                                            else
                                                            {
                                                                Toast.makeText(context, "Please enter backup name to go further.", Toast.LENGTH_LONG).show();
                                                            }

                                                        }
                                                    }
                                        );

    alertDialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
                                                        {
                                                            public void onClick(DialogInterface dialog,int id)
                                                            {
                                                                dialog.cancel();
                                                            }
                                                        }
                                        );

    AlertDialog alertDialog = alertDialogBuilder.create();
    alertDialog.show();
于 2013-04-30T11:21:00.557 回答
0

尝试这个 - -

替换这一行---

  mInput = ((EditText) LayoutInflater.from(context).inflate(R.layout.dialog_view, null).findViewById(R.id.etxtDialog)).getText().toString();

和 -

mInput = ((EditText) builder.findViewById(R.id.etxtDialog));
String input= mInput.getText().toString();
于 2013-04-30T11:21:22.747 回答