EditText .getText()方法没有返回任何值

时间:2018-03-21 16:12:32

标签: android

我遇到了一个从editText获取文本的简单问题。

fab1.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View v) {
    AlertDialog.Builder placeLLDialog= new AlertDialog.Builder(PlacesActivity.this);
    LayoutInflater inflater = getLayoutInflater();
    final View view = inflater.inflate(R.layout.place_add_dialog, null);
    placeLLDialog.setView(R.layout.place_add_dialog);

    final EditText place = view.findViewById(R.id.placeName);
    final EditText lati = view.findViewById(R.id.placeLati);
    final EditText longi = view.findViewById(R.id.placeLongi);
    placeLLDialog.setTitle("Add Place with Latitude and Longitude")
      .setPositiveButton("Add", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
          Log.e(TAG, "Helloooo" + place.getText().toString());

          if(!place.getText().toString().equals("") &&
             !lati.getText().toString().equals("") &&
             !longi.getText().toString().equals("")) {
               Log.e(TAG, "Hello" + place.getText().toString());
               final Places places = new Places(place.getText().toString(),
                 lati.getText().toString(), longi.getText().toString());
               mPlacesViewModel.insert(places);
          }
          closeFABMenu();
        }
      })
      .setNegativeButton("Cancel", null)
      .show();
  }

当我这样做时,我没有得到地方,拉提和朗吉的价值, 即“place.getText()。toString()”为空。

有人可以帮我解决这个奇怪的问题吗?

2 个答案:

答案 0 :(得分:0)

您在这里夸大视图:

final View view = inflater.inflate(R.layout.place_add_dialog, null);

但您之后只使用了一行:

placeLLDialog.setView(R.layout.place_add_dialog);

所以你应该设置:

placeLLDialog.setView(view);

答案 1 :(得分:0)

 fab1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder placeLLDialog = new AlertDialog.Builder(PlacesActivity.this);
                LayoutInflater inflater = getLayoutInflater();
                final View view = inflater.inflate(R.layout.place_add_dialog,null);
                placeLLDialog.setView(view);
                final EditText place=(EditText)view.findViewById(R.id.placeName);
                final EditText lati = (EditText)view.findViewById(R.id.placeLati);
                final EditText longi = (EditText)view.findViewById(R.id.placeLongi);
                placeLLDialog.setTitle("Add Place with Latitude and Longitude")
                        .setPositiveButton("Add", new  DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                Log.e(TAG, "Helloooo" + place.getText().toString());
                                if(!place.getText().toString().equals("") && !lati.getText().toString().equals("") && !longi.getText().toString().equals("")) {
                                    Log.e(TAG, "Hello" + place.getText().toString());
                                    final Places places = new  Places(place.getText().toString(),lati.getText().toString(), longi.getText().toString()); 
                                    mPlacesViewModel.insert(places);
                                }
                                closeFABMenu();
                            }
                        })
                        .setNegativeButton("Cancel", null)
                        .show();
            }
        });