2013-02-12 12 views
10

मेरे पास एक अलर्टडिअलॉग वाला एक एप्लिकेशन है जो एक एकल संपादन टेक्स्ट दिखाता है। मैंने इसे करने के लिए एंड्रॉइड डेवलपर्स गाइड का पालन किया, लेकिन मुझे नहीं पता कि उपयोगकर्ता द्वारा दर्ज डेटा कैसे प्राप्त किया जाए।कस्टम दृश्य के साथ एंड्रॉइड अलर्टडियलॉग: इनपुट डेटा प्राप्त करें

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

मैं संवाद बनाने के लिए एक DialogFragment उपयोग कर रहा हूँ:

कस्टम लेआउट केवल एक EditText है। जब उपयोगकर्ता ठीक बटन पर क्लिक करता है तो डेटा वापस पाने के लिए मैं एक इंटरफ़ेस का उपयोग कर रहा हूं।

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

    } 
} 

मेरी गतिविधि में मैं निम्न कार्य करें:

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

EditText मैं DialogFragment अंदर प्राप्त करने का प्रयास हमेशा शून्य है। मैं EditText का मान कैसे प्राप्त कर सकता हूं?

धन्यवाद!

उत्तर

21

मुझे लगता है कि ऐसा इसलिए है क्योंकि आप जिस दृश्य में अपना संपादन ढूंढने की कोशिश कर रहे हैं वह सही नहीं है।

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

कि यह था:

वह कुछ इस तरह होना चाहिए। धन्यवाद! –

+0

मैंने कोशिश की लेकिन लाइन पर, जहां valueView प्रारंभ किया गया है, संकलक मुझे एक त्रुटि देता है: "एक अलग विधि में परिभाषित एक आंतरिक वर्ग के अंदर एक गैर-अंतिम परिवर्तनीय diag_view का संदर्भ नहीं दे सकता"। यदि मैं संवाद करता हूं तो अंतिम चर देखें, इसे बदला नहीं जाएगा और इससे कुछ भी पुनर्प्राप्त नहीं किया जा सकता है। – jerryh91

+0

तो, वर्ग फ़ील्ड के रूप में, विधि के बाहर 'देखें संवाद देखें' घोषित करें। –

संबंधित मुद्दे