6

का उपयोग कर टेक्स्टव्यू और एडिटटेक्स्ट को कैसे जोड़ना है मैं डिफ़ॉल्ट अलर्टडिअलॉग में दो तत्व जोड़ने की कोशिश कर रहा हूं लेकिन मुझे यह काम नहीं लग रहा है। यहाँ मेरी कोड है:डिफ़ॉल्ट अलर्टडिअलॉग प्रोग्रामेटिक

// START Dialog 
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this); 

    TextView tv = new TextView(this); 
    tv.setText(title); 
    tv.setPadding(40, 40, 40, 40); 
    tv.setGravity(Gravity.CENTER); 
    tv.setTextSize(20); 

    EditText et = new EditText(this); 
    etStr = et.getText().toString(); 

    alertDialogBuilder.setView(et); 
    alertDialogBuilder.setTitle(title); 
    alertDialogBuilder.setMessage("Input Student ID"); 
    alertDialogBuilder.setCustomTitle(tv); 

    if (isError) 
     alertDialogBuilder.setIcon(R.drawable.icon_warning); 
    // alertDialogBuilder.setMessage(message); 
    alertDialogBuilder.setCancelable(false); 

    // Setting Negative "Cancel" Button 
    alertDialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int whichButton) { 
      dialog.cancel(); 
     } 
    }); 

    // Setting Positive "Yes" Button 
    alertDialogBuilder.setPositiveButton("OK", 
      new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int which) { 
        if (isError) 
         finish(); 
        else { 
         Intent intent = new Intent(
           ChangeDeviceActivity.this, 
           MyPageActivity.class); 
         startActivity(intent); 
        } 
       } 
      }); 

    AlertDialog alertDialog = alertDialogBuilder.create(); 

    try { 
     alertDialog.show(); 
    } catch (Exception e) { 
     // WindowManager$BadTokenException will be caught and the app would 
     // not display the 'Force Close' message 
     e.printStackTrace(); 
    } 

अभी के लिए, यह केवल एक संदेश alertDialogBuilder.setMessage("Input Student ID"); द्वारा निर्धारित के साथ एक EditText है, लेकिन मैं इस एक TextView तो मैं कर सकते हैं यह केंद्र का औचित्य साबित करना चाहते हैं। मैं यह कैसे करु?

उत्तर

23
 AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this); 

     LinearLayout layout = new LinearLayout(this); 
     LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); 
     layout.setOrientation(LinearLayout.VERTICAL); 
     layout.setLayoutParams(parms); 

     layout.setGravity(Gravity.CLIP_VERTICAL); 
     layout.setPadding(2, 2, 2, 2); 

     TextView tv = new TextView(this); 
     tv.setText("Text View title"); 
     tv.setPadding(40, 40, 40, 40); 
     tv.setGravity(Gravity.CENTER); 
     tv.setTextSize(20); 

     EditText et = new EditText(this); 
     etStr = et.getText().toString(); 
     TextView tv1 = new TextView(this); 
     tv1.setText("Input Student ID"); 

     LinearLayout.LayoutParams tv1Params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); 
     tv1Params.bottomMargin = 5; 
     layout.addView(tv1,tv1Params); 
     layout.addView(et, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)); 

     alertDialogBuilder.setView(layout); 
     alertDialogBuilder.setTitle(title); 
     // alertDialogBuilder.setMessage("Input Student ID"); 
     alertDialogBuilder.setCustomTitle(tv); 

     if (isError) 
      alertDialogBuilder.setIcon(R.drawable.icon_warning); 
     // alertDialogBuilder.setMessage(message); 
     alertDialogBuilder.setCancelable(false); 

     // Setting Negative "Cancel" Button 
     alertDialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int whichButton) { 
       dialog.cancel(); 
      } 
     }); 

     // Setting Positive "OK" Button 
     alertDialogBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int which) { 
       if (isError) 
        finish(); 
       else { 
         Intent intent = new Intent(ChangeDeviceActivity.this, 
         MyPageActivity.class); startActivity(intent); 
       } 
      } 
     }); 

     AlertDialog alertDialog = alertDialogBuilder.create(); 

     try { 
      alertDialog.show(); 
     } catch (Exception e) { 
      // WindowManager$BadTokenException will be caught and the app would 
      // not display the 'Force Close' message 
      e.printStackTrace(); 
     } 
+0

हालांकि यह काम करता है कि मैं 'tv1' को संपादन टेक्स्ट के शीर्ष पर होना चाहता था इसलिए मैंने 'layout.setOrientation (LinearLayout.HORIZONTAL) बदल दिया;' 'लेआउट.सेटऑरिएंटेशन (LinearLayout.VERTICAL);' लेकिन दोनों' tv1' और 'et 'प्रदर्शित नहीं है? –

+1

अब जांचें कि मैं ऊर्ध्वाधर अभिविन्यास के लिए उत्तर संपादित करता हूं। –

+0

बहुत बहुत धन्यवाद! मैं सचमुच इसकी सराहना करता हूं। संपादित भाग के लिए +1। –

1

setView() पर कॉल करते समय मूल TextView संदेश धारण हो जाता है। आप

की जरूरत
  • लेआउट
  • findViewById()
  • साथ विचारों का संदर्भ मिलता है एक छोटा सा लेआउट एक्सएमएल बनाने और इसे
  • को EditText और एक TextView डाल बढ़ दृश्यों के साथ कुछ करना
+0

लेकिन इसके साथ, मुझे एंड्रॉइड डिफ़ॉल्ट अलर्ट संवाद लेआउट या होलो लाइट थीम नहीं मिल सका। –

+0

क्यों नहीं? डिफ़ॉल्ट AlertDialog विषय बाहर सेट है। आप बस एक सादे संपादन टेक्स्ट के बजाय बच्चों के साथ एक दृश्य पास करते हैं। – flx

2

एक .xml फ़ाइल बनाएं जिसमें आप जो भी दृश्य देखना चाहते हैं उसे शामिल करें।

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="vertical"> 

<TextView 
     android:id="@+id/textView1" 
     android:layout_width="312dp" 
     android:layout_height="wrap_content" 
     android:text="Enter your email address :" 
     android:layout_marginLeft="5dp" 
     android:textAppearance="?android:attr/textAppearanceSmall"/> 

<EditText 
     android:id="@+id/dialog1Edittext" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:ems="10" 
     android:layout_marginTop="10dp" 
     android:inputType="textEmailAddress"> 

    <requestFocus/> 
</EditText> 

कोड निम्नलिखित अपने जावा फाइल औजार में उसके बाद।

View view = View.inflate(this, R.layout.yourxmlname, null); 
AlertDialog.Builder alert = new AlertDialog.Builder(this); 
// Now set the dialog's content 
alert.setContentView(view); 

आशा है कि यह आपकी मदद करे।

+0

इसके लिए धन्यवाद लेकिन मैं डिफ़ॉल्ट अलर्ट संवाद का उपयोग करना चाहता था इसलिए मुझे इसे प्रोग्रामेटिक रूप से जोड़ना पड़ा। –

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