How to make a edittext box in a dialog

后端 未结 8 1829
借酒劲吻你
借酒劲吻你 2020-11-28 01:34

I am trying to make a edittext box in a dialog box for entering a password. and when I am doing I am not able to do. I am a beginner in it. Please help me in this.



        
相关标签:
8条回答
  • 2020-11-28 01:44

    Wasim's answer lead me in the right direction but I had to make some changes to get it working for my current project. I am using this function in a fragment and calling it on button click.

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

    I hope it helps someone else in the near future. Happy coding!

    0 讨论(0)
  • 2020-11-28 01:45

    Use Activtiy Context

    Replace this

      final EditText input = new EditText(this);
    

    By

      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
    
    0 讨论(0)
  • 2020-11-28 01:45

    Simplest of all would be.

    • Create xml layout file for dialog . Add whatever view you want like EditText , ListView , Spinner etc.

      Inflate this view and set this to AlertDialog

    Lets start with Layout file first.

    <?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();
    
    0 讨论(0)
  • 2020-11-28 01:48

    Try below code:

    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);
    
    0 讨论(0)
  • 2020-11-28 01:54

    Simplified version

    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;
    
    0 讨论(0)
  • 2020-11-28 01:54

    You can also create custom alert dialog by creating an xml file.

    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" />
    

    The Java Code:

    @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 讨论(0)
提交回复
热议问题