Android:EditText.getText()。toString()不起作用

时间:2013-04-30 11:11:04

标签: java android android-edittext android-alertdialog

我使用此代码构建一个带有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确实包含一些字符。为什么这段代码不起作用?

5 个答案:

答案 0 :(得分:7)

你做错了......抓住你的膨胀视图的实例,稍后再使用它。例如:

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

答案 1 :(得分: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是您的应用主题

答案 2 :(得分:1)

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

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

答案 3 :(得分: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();

答案 4 :(得分: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();