在Alert对话框中访问LinearLayout中的EditText

时间:2017-04-11 14:05:30

标签: java android android-studio

我有这段代码:

final AlertDialog.Builder alert = new AlertDialog.Builder(this);
final EditText name = new EditText(this);
final EditText surname = new EditText(this);
final EditText email = new EditText(this);
LinearLayout ll=new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
ll.addView(image);
ll.addView(name);
ll.addView(surname);
ll.addView(email);
alert.setView(ll);
alert.setPositiveButton(R.string.enter, new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
        String name = // ??
        String surname = // ??
        String email = // ??
    }
});
alert.show();

如何获取OnClickListener内的EditText字段输入的文字字符串?

1 个答案:

答案 0 :(得分:1)

试试这个

  final AlertDialog.Builder alert = new AlertDialog.Builder(this);
  final EditText name = new EditText(this);
  final EditText surname = new EditText(this);
  final EditText email = new EditText(this);
  LinearLayout ll=new LinearLayout(this);
  ll.setOrientation(LinearLayout.VERTICAL);
  ll.addView(image);
  ll.addView(name);
  ll.addView(surname);
  ll.addView(email);
  alert.setView(ll);
  alert.setPositiveButton(R.string.enter, new                 
  DialogInterface.OnClickListener() {
      public void onClick(DialogInterface dialog, int whichButton) {
      String name_txt = name.getText().toString();
      String surname_txt = surname.getText().toString();
      String email_txt = email.getText().toString();
   }
      });
  alert.show();