एक संवाद में एक संपादन बॉक्स कैसे बनाया जाए


211

मैं पासवर्ड दर्ज करने के लिए एक संवाद बॉक्स में एक संपादन बॉक्स बनाने की कोशिश कर रहा हूं। और जब मैं कर रहा हूं तो मैं नहीं कर पा रहा हूं। मैं इसमें एक शुरुआत हूं। कृपया इस संबंध में मेरी सहायता करें।

public class MainActivity extends Activity {

Button create, show, setting;
//String pass="admin";String password;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    create = (Button)findViewById(R.id.amcreate);
    setting = (Button)findViewById(R.id.amsetting);
    show = (Button)findViewById(R.id.amshow);
    //input = (EditText)findViewById(R.id.this);

    setting.setVisibility(View.INVISIBLE);

    create.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            Intent myIntent1 = new Intent(view.getContext(), Create.class);
            startActivityForResult(myIntent1, 0);
        }

    });

    show.setOnClickListener(new View.OnClickListener() {
        //@SuppressWarnings("deprecation")
        public void onClick(final View view) {

            // Creating alert Dialog with one Button
            AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);

            //AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();

            // Setting Dialog Title
            alertDialog.setTitle("PASSWORD");

            // Setting Dialog Message
            alertDialog.setMessage("Enter Password");
            **final EditText input = new EditText(this);**
            //alertDialog.setView(input);

            // Setting Icon to Dialog
            alertDialog.setIcon(R.drawable.key);

            // Setting Positive "Yes" Button
            alertDialog.setPositiveButton("YES",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,int which) {
                            // Write your code here to execute after dialog
                            Toast.makeText(getApplicationContext(),"Password Matched", Toast.LENGTH_SHORT).show();
                            Intent myIntent1 = new Intent(view.getContext(), Show.class);
                            startActivityForResult(myIntent1, 0);
                        }
                    });
            // Setting Negative "NO" Button
            alertDialog.setNegativeButton("NO",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            // Write your code here to execute after dialog
                            dialog.cancel();
                        }
                    });

            // closed

            // Showing Alert Message
            alertDialog.show();
        }

    }); 

छवि

यहां छवि विवरण दर्ज करें

मैं जैसा चाहता हूं

यहां छवि विवरण दर्ज करें

 AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
 alertDialog.setTitle("PASSWORD");
 alertDialog.setMessage("Enter Password");

 final EditText input = new EditText(MainActivity.this);
 LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
     LinearLayout.LayoutParams.MATCH_PARENT,
     LinearLayout.LayoutParams.MATCH_PARENT);
 input.setLayoutParams(lp);
 alertDialog.setView(input);
 alertDialog.setIcon(R.drawable.key);

 alertDialog.setPositiveButton("YES",
     new DialogInterface.OnClickListener() {
         public void onClick(DialogInterface dialog, int which) {
             password = input.getText().toString();
             if (password.compareTo("") == 0) {
                 if (pass.equals(password)) {
                     Toast.makeText(getApplicationContext(),
                         "Password Matched", Toast.LENGTH_SHORT).show();
                     Intent myIntent1 = new Intent(view.getContext(),
                         Show.class);
                     startActivityForResult(myIntent1, 0);
                 } else {
                     Toast.makeText(getApplicationContext(),
                         "Wrong Password!", Toast.LENGTH_SHORT).show();
                 }
             }
         }
     });

 alertDialog.setNegativeButton("NO",
     new DialogInterface.OnClickListener() {
         public void onClick(DialogInterface dialog, int which) {
             dialog.cancel();
         }
     });

 alertDialog.show();
 }

 });

1
इस डेवलपर की जांच करें ।android.com / guide / topics / ui / notifiers / toasts.html । स्थिति की जाँच करें। लेकिन मुझे लगता है कि एडिटटेक्स्ट के लिए सेटर के लिए बेहतर है
रघुनंदन

जवाबों:


171

एक्टीविटी कॉन्सेप्ट का इस्तेमाल करें

इसे बदलें

  final EditText input = new EditText(this);

द्वारा

  final EditText input = new EditText(MainActivity.this);  
  LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.MATCH_PARENT,
                        LinearLayout.LayoutParams.MATCH_PARENT);
  input.setLayoutParams(lp);
  alertDialog.setView(input); // uncomment this line

1
@ अभिषेक व्हेयर आपकी इनिशियलाइज़ेशन है। की तरह Dialog dialog = new Dialog(MainActivity.this)। मुझे लगता है कि आपने अपने कोड को दूसरे स्थान से कॉपी किया है जहां मुझे लगता है
रघुनंदन

1
@ अभिषेक माफ करना मेरी उलझन यह है कि आपने कस्टम डायलॉग सोचा था। public void onClick(DialogInterface dialogइसके संवादकर्ता हैं। का उपयोग कर एक समस्या है कि आप नकारात्मक बटन को क्लिक करने के लिए alertdialog को खारिज नहीं है।
रघुनंदन

1
@ अभिषेक एक टोस्ट संदेश प्रदर्शित करता है या संपादन के लिए त्रुटि सेट करता है। stackoverflow.com/questions/7747268/…
रघुनंदन

1
टोस्ट टोस्ट = टोस्ट.makeText (MainActivity.this, "कृपया पासवर्ड दर्ज करें!", टोस्ट.LENGTH_LONG); टोस्ट.सेटगैविटी (ग्रेविटी.केंटर, 0, 0); toast.show (); इसने मुझे केंद्र में टोस्ट प्राप्त करने के लिए काम किया।
अब्बू

5
हाय रघु, अगर मैं इस edittext बॉक्स में बायां मार्जिन और दायां मार्जिन डालना चाहता हूं तो मुझे क्या लिखना चाहिए? मैंने कई उत्तर देने की कोशिश की, मार्जिन को निर्धारित करने के लिए, लेकिन कुछ भी काम नहीं किया :(
लूसिफ़ेर

287

मुझे इस प्रश्न का उत्तर देने में बहुत देर हो चुकी है, लेकिन दूसरों के लिए जो इस तरह की किसी चीज़ की खोज कर रहे हैं, एक एडिटबॉक्स के साथ एक अलर्टबॉक्स का एक सरल कोड है

AlertDialog.Builder alert = new AlertDialog.Builder(this); 

या

new AlertDialog.Builder(mContext, R.style.MyCustomDialogTheme);

यदि आप संवाद का विषय बदलना चाहते हैं।

final EditText edittext = new EditText(ActivityContext);
alert.setMessage("Enter Your Message");
alert.setTitle("Enter Your Title");

alert.setView(edittext);

alert.setPositiveButton("Yes Option", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
        //What ever you want to do with the value
        Editable YouEditTextValue = edittext.getText();
        //OR
        String YouEditTextValue = edittext.getText().toString();
    }
});

alert.setNegativeButton("No Option", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
        // what ever you want to do with No option.
    }
});

alert.show();

मुझे यह कोड कहां रखना चाहिए? जब मैंने इसे विधि में आज़माया, और अंतिम फ़ील्ड EditText को कक्षा के ऊपर रखा, तो यह क्रैश हो जाता है।
सेरबिन

दुर्घटना क्या है?
सायदा ज़ूनैरा

10
इस EditText को मार्जिन कैसे दें?
घनश्याम नयमा

1
पूछे गए विषय का अच्छा सुझाव है, लेकिन एक शैली टिप्पणी की जरूरत है। एंड्रॉइड विंडोज नहीं है, और रद्द बटन की आवश्यकता नहीं है जैसे कि Win32 में यह आवश्यक है, ओएस "बैक" बटन है जो रद्द / नहीं के रूप में कार्य करता है। इसलिए मेरा सुझाव पासवर्ड अनुरोध संवाद पर नकारात्मक बटन को छोड़ रहा है, और सकारात्मक बटन "हां" नहीं होना चाहिए, लेकिन "ओके" होना चाहिए (और android.R.string.ok का उपयोग करके इसे स्थानीयकृत करें)। Stackoverflow.com/questions/11459827// विषय पर मेरे उत्तर में और देखें ।
Jan Bergström

2
संपादित फ़ील्ड में एक पासवर्ड अनुरोध में एक लाइनर (edittext.setSingleLine ();) भी होना चाहिए (क्योंकि यह है) और यह एक संलग्न भौतिक (बीटी) कीबोर्ड (या एक Chromebook) पर दबाव डालने से फोकस कूदता है। अगले आइटम, सकारात्मक बटन। अर्थ यह है कि पाठ में प्रवेश करने के बाद, दो बार प्रवेश धकेलने से संवाद सकारात्मक रूप से समाप्त हो जाता है।
Jan Bergström

36

सब से सरल होगा।

  • संवाद के लिए xml लेआउट फ़ाइल बनाएँ। एडिट टेक्स्ट, लिस्ट व्यू, स्पिनर इत्यादि जो भी आप चाहते हैं उसे जोड़ें।

    इस दृश्य को देखें और इसे AlertDialog पर सेट करें

पहले लेआउट फ़ाइल से शुरू करें।

<?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:gravity="center_horizontal"
    android:orientation="vertical">


    <EditText
        android:id="@+id/etComments"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="top"
        android:hint="Enter comments(Optional)"
        android:inputType="textMultiLine"
        android:lines="8"
        android:maxLines="3"
        android:minLines="6"
        android:scrollbars="vertical" />

</LinearLayout>

final View view = layoutInflater.inflate(R.layout.xml_file_created_above, null);
AlertDialog alertDialog = new AlertDialog.Builder(ct).create();
alertDialog.setTitle("Your Title Here");
alertDialog.setIcon("Icon id here");
alertDialog.setCancelable(false);
Constant.alertDialog.setMessage("Your Message Here");


final EditText etComments = (EditText) view.findViewById(R.id.etComments);

alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "OK", new OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {

    }
});


alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "Cancel", new OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        alertDialog.dismiss()
    }
});


alertDialog.setView(view);
alertDialog.show();

3
आप क्या हीरो हैं?
एडम मैकक्विफ

मैं दुर्घटना। मुझे लगता है कि मैंने दुर्घटनाग्रस्त होने का कारण ढूंढ लिया था, मुझे खोज दृश्य में 'दृश्य' याद आ गया था। अब मुझे सिर्फ यह जानना है कि चेक बॉक्स को कैसे संभालना है ताकि मैं पासवर्ड दिखा सकूं या छिपा सकूं। डायलॉग अभी भी जीवित है। बहु आइटम दृष्टिकोण की कोशिश की, लेकिन इसने अपने लेआउट में एक के अलावा अपना स्वयं का चेक बॉक्स जोड़ा!
ब्रायन रेनहोल्ड

23

सरलीकृत संस्करण

final EditText taskEditText = new EditText(this);
AlertDialog dialog = new AlertDialog.Builder(this)
        .setTitle("Add a new task")
        .setMessage("What do you want to do next?")
        .setView(taskEditText)
        .setPositiveButton("Add", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                String task = String.valueOf(taskEditText.getText());
                SQLiteDatabase db = mHelper.getWritableDatabase();
                ContentValues values = new ContentValues();
                values.put(TaskContract.TaskEntry.COL_TASK_TITLE, task);
                db.insertWithOnConflict(TaskContract.TaskEntry.TABLE,
                        null,
                        values,
                        SQLiteDatabase.CONFLICT_REPLACE);
                db.close();
                updateUI();
            }
        })
        .setNegativeButton("Cancel", null)
        .create();
dialog.show();
return true;

12

कोड के नीचे आज़माएं:

alert.setTitle(R.string.WtsOnYourMind);

 final EditText input = new EditText(context);
 input.setHeight(100);
 input.setWidth(340);
 input.setGravity(Gravity.LEFT);

 input.setImeOptions(EditorInfo.IME_ACTION_DONE);
 alert.setView(input);

5

लेआउट पैराम्स में मार्जिन सेट करने से Alertdialog में काम नहीं होगा। आपको पैरेंट लेआउट में पैडिंग सेट करना होगा और फिर उस लेआउट में एडिटटेक्स्ट जोड़ना होगा।

यह मेरा काम करने का कोडिन कोड है ...

val alert =  AlertDialog.Builder(context!!)

val edittext = EditText(context!!)
edittext.hint = "Enter Name"
edittext.maxLines = 1

val layout = FrameLayout(context!!)

//set padding in parent layout
layout.setPaddingRelative(45,15,45,0)

alert.setTitle(title)

layout.addView(edittext)

alert.setView(layout)

alert.setPositiveButton(getString(R.string.label_save), DialogInterface.OnClickListener {

    dialog, which ->
    run {

        val qName = edittext.text.toString()

        Utility.hideKeyboard(context!!, dialogView!!)

    }

})
alert.setNegativeButton(getString(R.string.label_cancel), DialogInterface.OnClickListener {

            dialog, which ->
            run {
                dismiss()
            }

})

alert.show()

4

आप xml फ़ाइल बनाकर कस्टम अलर्ट डायलॉग भी बना सकते हैं।

dialoglayout.xml

   <EditText
    android:id="@+id/dialog_txt_name"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:hint="Name"
    android:singleLine="true" >

    <requestFocus />
  </EditText>
   <Button
        android:id="@+id/btn_login"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="60dp"
        android:background="@drawable/red"
        android:padding="5dp"
        android:textColor="#ffffff"
        android:text="Submit" />

    <Button
        android:id="@+id/btn_cancel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_toRightOf="@+id/btn_login"
        android:background="@drawable/grey"
        android:padding="5dp"
        android:text="Cancel" />

जावा कोड:

@Override//to popup alert dialog
public void onClick(View arg0) {
    // TODO Auto-generated method stub
    showDialog(DIALOG_LOGIN);
});


@Override
protected Dialog onCreateDialog(int id) {
    AlertDialog dialogDetails = null;

    switch (id) {
        case DIALOG_LOGIN:
            LayoutInflater inflater = LayoutInflater.from(this);
            View dialogview = inflater.inflate(R.layout.dialoglayout, null);
            AlertDialog.Builder dialogbuilder = new AlertDialog.Builder(this);
            dialogbuilder.setTitle("Title");
            dialogbuilder.setView(dialogview);
            dialogDetails = dialogbuilder.create();
            break;
    }
    return dialogDetails;
}

@Override
protected void onPrepareDialog(int id, Dialog dialog) {
    switch (id) {
        case DIALOG_LOGIN:
             final AlertDialog alertDialog = (AlertDialog) dialog;
             Button loginbutton = (Button) alertDialog
                .findViewById(R.id.btn_login);
             Button cancelbutton = (Button) alertDialog
                .findViewById(R.id.btn_cancel);
             userName = (EditText) alertDialog
                .findViewById(R.id.dialog_txt_name);
             loginbutton.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {
                      String name = userName.getText().toString();
                      Toast.makeText(Activity.this, name,Toast.LENGTH_SHORT).show();
             });
             cancelbutton.setOnClickListener(new View.OnClickListener() {
                      @Override
                      public void onClick(View v) {
                           alertDialog.dismiss();
                      }
             });
             break;
   }
}

यह वही है जिसकी मुझे आवश्यकता है लेकिन मैं इसे अपनी त्रुटि दिखाने में कैसे उपयोग कर सकता हूं
राजू

0

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

fun showPostDialog(title: String) {
        val alert =  AlertDialog.Builder(activity)

        val edittext = EditText(activity)
        edittext.hint = "Enter Name"
        edittext.maxLines = 1

        var layout = activity?.let { FrameLayout(it) }

        //set padding in parent layout
//        layout.isPaddingRelative(45,15,45,0)
        layout?.setPadding(45,15,45,0)

        alert.setTitle(title)

        layout?.addView(edittext)

        alert.setView(layout)

        alert.setPositiveButton(getString(R.string.label_save), DialogInterface.OnClickListener {

                dialog, which ->
            run {

                val qName = edittext.text.toString()

                showToast("Posted to leaderboard successfully")

                view?.hideKeyboard()

            }

        })
        alert.setNegativeButton(getString(R.string.label_cancel), DialogInterface.OnClickListener {

                dialog, which ->
            run {
                dialog.dismiss()
            }

        })

        alert.show()
    }

    fun View.hideKeyboard() {
        val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        imm.hideSoftInputFromWindow(windowToken, 0)
    }

    fun showToast(message: String) {
        Toast.makeText(activity, message, Toast.LENGTH_LONG).show()
    }

मुझे आशा है कि यह निकट भविष्य में किसी और की मदद करेगा। हैप्पी कोडिंग!

हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.