2010-07-20 17 views
6

का उपयोग कर AlertDialog बिल्डर में EditTexts के मान को पुनर्प्राप्त करना निम्न कोड स्निपेट का उपयोग करके, मैं टेक्स्ट मान प्राप्त करने का प्रयास कर रहा हूं जिसे EditText एस में टाइप किया गया है।लेआउट

LayoutInflater factory = LayoutInflater.from(this); 
final View loginView = factory.inflate(R.layout.login, null); 

b.setOnClickListener(new OnClickListener() { 
    public void onClick(View v) { 
     Dialog d = new AlertDialog.Builder(NewOrder.this) 
      .setIcon(R.drawable.icon) 
      .setTitle("Log In") 
      .setView(loginView) 
      .setPositiveButton("Log In", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int whichButton) { 
        mSpinner = ProgressDialog.show(mContext, "", "Authenticating User..."); 
        new LoginTask().execute(); 
       } 
      }) 
      .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int whichButton) { 
        /* User clicked cancel so do some stuff */ 
       } 
      }) 
      .create(); 
     d.show(); 
    } 
}); 

मेरे login.xml सुंदर मानक है और सीधे आगे:

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"> 

    <TextView 
     android:id="@+id/username_view" 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:layout_marginLeft="20dip" 
     android:layout_marginRight="20dip" 
     android:text="Email Address" 
     android:gravity="left" 
     android:textAppearance="?android:attr/textAppearanceMedium" /> 

    <EditText 
     android:id="@+id/username" 
     android:layout_height="wrap_content" 
     android:layout_width="fill_parent" 
     android:layout_marginLeft="20dip" 
     android:layout_marginRight="20dip" 
     android:scrollHorizontally="true" 
     android:autoText="false" 
     android:capitalize="none" 
     android:gravity="fill_horizontal" 
     android:textAppearance="?android:attr/textAppearanceMedium" /> 

    <TextView 
     android:id="@+id/password_view" 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:layout_marginLeft="20dip" 
     android:layout_marginRight="20dip" 
     android:text="Password" 
     android:gravity="left" 
     android:textAppearance="?android:attr/textAppearanceMedium" /> 

    <EditText 
     android:id="@+id/password" 
     android:layout_height="wrap_content" 
     android:layout_width="fill_parent" 
     android:layout_marginLeft="20dip" 
     android:layout_marginRight="20dip" 
     android:scrollHorizontally="true" 
     android:autoText="false" 
     android:capitalize="none" 
     android:gravity="fill_horizontal" 
     android:password="true" 
     android:textAppearance="?android:attr/textAppearanceMedium" /> 

</LinearLayout> 

किसी PositiveButton क्लिक करता है, मैं कैसे EditText क्षेत्रों के मूल्य मिलता है?

उत्तर

1
.setPositiveButton("Log In", new DialogInterface.OnClickListener() { 
    public void onClick(DialogInterface dialog, int whichButton) { 
     mSpinner = ProgressDialog.show(mContext, "", "Authenticating  User..."); 

     /* == Get EditText Values Example == */ 
     EditText edtext_username= (EditText) findViewById(R.id.username); 
     EditText edtext_password= (EditText) findViewById(R.id.password); 

     String ValueofUsername = edtext_username.getText().toString(); 
     String ValueofPassword = edtext_password.getText().toString(); 
     /* ========== End Example ========== */ 

     new LoginTask().execute(); 
    } 
}) 
+0

मुझे नहीं लगता कि हम 'ऑनक्लिक लिस्टनर' अज्ञात वर्ग में 'findViewById' को सीधे कॉल कर सकते हैं। आपको पहले से ही संदर्भ दृश्य 'loginView' का उपयोग करना होगा। विवरण के लिए कृपया मेरा उत्तर देखें। –

0

मुझे यकीन है कि आप सीधे गुमनाम कक्षा में findViewById() ऐसे ही कॉल कर सकते हैं के रूप में स्वीकार जवाब पता चलता है नहीं कर रहा हूँ। मेरे लिए यह देता है -

The method findViewById() is undefined for the type new DialogInterface.OnClickListener(){} 

आप View loginView कि आप पहले से ही करने के लिए संदर्भित किया है पर कॉल करने के लिए की है। यह भी महत्वपूर्ण है loginView को अज्ञात वर्ग OnClickListener में संदर्भित करने के लिए अंतिम के रूप में घोषित किया जाना चाहिए।

final View loginView = factory.inflate(R.layout.login, null); 
// loginView should be declared final 
b.setOnClickListener(new OnClickListener() { 
    public void onClick(View v) { 
     Dialog d = new AlertDialog.Builder(NewOrder.this) 
      .setIcon(R.drawable.icon) 
      .setTitle("Log In") 
      .setView(loginView) 
      .setPositiveButton("Log In", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int whichButton) { 
        mSpinner = ProgressDialog.show(mContext, "", "Authenticating User..."); 
        EditText usernameText= (EditText) loginView.findViewById(R.id.username); 
        EditText passwordText= (EditText) loginView.findViewById(R.id.password); 
        new LoginTask().execute(); 
       } 
      }) 
      //more code 
+0

कृपया नीचे वोट के लिए कारण? –

+0

यदि आप एक संवाद बॉक्स के लिए कस्टम क्लास का उपयोग नहीं कर रहे हैं और जाने पर संवाद बॉक्स बना रहे हैं तो यह अधिक स्वीकार्य है – Naveen

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