2013-02-12 37 views
10

Tôi có một ứng dụng với một AlertDialog hiển thị một EditText duy nhất. Tôi đã làm theo hướng dẫn dành cho nhà phát triển Android để làm điều đó, nhưng tôi không thể tìm cách lấy dữ liệu do người dùng nhập.Android AlertDialog với chế độ xem tùy chỉnh: nhận dữ liệu đầu vào

Cách bố trí tùy chỉnh chỉ có một EditText:

<EditText 
    android:id="@+id/license_value" 
    android:inputType="text" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:hint="@string/license" /> 

Tôi đang sử dụng một DialogFragment để tạo ra các hộp thoại. Để lấy lại dữ liệu khi người dùng nhấp vào nút ok Tôi đang sử dụng giao diện.

public class EditLicenseDialogFragment extends DialogFragment { 

    public interface EditLicenseDialogListener { 
     public void onDialogPositiveClick(DialogFragment dialog, String value); 
    } 

    private EditLicenseDialogListener mListener; 

    // Override the Fragment.onAttach() method to instantiate the NoticeDialogListener 
    @Override 
    public void onAttach(Activity activity) { 
     super.onAttach(activity); 
     // Verify that the host activity implements the callback interface 
     try { 
      // Instantiate the NoticeDialogListener so we can send events to the host 
      mListener = (EditLicenseDialogListener) activity; 
     } catch (ClassCastException e) { 
      // The activity doesn't implement the interface, throw exception 
      throw new ClassCastException(activity.toString() 
        + " must implement NoticeDialogListener"); 
     } 
    } 

    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 
     // Use the Builder class for convenient dialog construction 
     AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 

     builder.setTitle(R.string.new_license); 

     LayoutInflater inflater = getActivity().getLayoutInflater(); 

     builder.setView(inflater.inflate(R.layout.edit_license, null)) 

     .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int id) { 
         // GET VALUE. HOW TO DO IT? 
       EditText valueView = (EditText) getActivity().findViewById(R.id.license_value); 
       if(valueView == null) Log.d("AA", "NULL"); 
       else{ 
        String value = valueView.getText().toString(); 
        mListener.onDialogPositiveClick(EditLicenseDialogFragment.this, value); 
       } 
      }) 
     .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int id) { 
       EditLicenseDialogFragment.this.getDialog().cancel(); 
      } 
     }); 

     return builder.create(); 

    } 
} 

Trong hoạt động của tôi, tôi làm như sau:

public class LicenseListActivity extends FragmentActivity 
    implements EditLicenseDialogFragment.EditLicenseDialogListener{ 

    ... 

    findViewById(R.id.buttonAddLicense).setOnClickListener(
      new View.OnClickListener() { 
       public void onClick(View view) { 
        DialogFragment dialog = new EditLicenseDialogFragment(true); 
        dialog.show(getSupportFragmentManager(), "EditLicenseDialogFragment"); 
       } 
      }); 

    @Override 
    public void onDialogPositiveClick(DialogFragment dialog, String value) { 
     Log.d("TAG", value); 
    } 

Các EditText Tôi cố gắng lấy lại bên trong DialogFragment luôn là NULL. Làm thế nào tôi có thể nhận được giá trị của EditText?

Cảm ơn!

Trả lời

21

Tôi nghĩ đó là vì chế độ xem mà bạn đang cố gắng tìm văn bản của mình không chính xác.

Nó phải là một cái gì đó như thế này:

LayoutInflater inflater = getActivity().getLayoutInflater(); 

View dialogView = inflater.inflate(R.layout.edit_license, null); 
builder.setView(dialogView) 
.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int id) { 

      EditText valueView = (EditText) dialogView.findViewById(R.id.license_value); //here 
      if(valueView == null) Log.d("AA", "NULL"); 
      else{ 
       String value = valueView.getText().toString(); 
       mListener.onDialogPositiveClick(EditLicenseDialogFragment.this, value); 
      } 
     }) 
+0

Đó là nó. Cảm ơn! –

+0

Tôi đã thử điều này nhưng trên dòng, nơi valueView được khởi tạo, trình biên dịch cho tôi một lỗi: "Không thể tham khảo một biến không phải là cuối cùng diag_view bên trong một lớp bên trong được định nghĩa trong một phương pháp khác nhau". Nếu tôi làm cho dialogView biến cuối cùng, nó sẽ không bị thay đổi và không có gì có thể được lấy ra từ nó. – jerryh91

+0

Vì vậy, hãy khai báo 'Xem dialogView' bên ngoài một phương thức, như là một trường lớp. –

Các vấn đề liên quan