使用布局参数限制 AlertDialog 中 EditText 的宽度?

Limit the width of an EditText within an AlertDialog with Layout Params?

问题:

假设

次要细节:

困惑:

代码:

    private void show_activation_prompt()
      {    

        // =============================================================
        // ======== create EditText which filters text =================
        // =============================================================

        // filter for alphanumerics, insert dashes during entry, capitalize, and turn off suggestions
        final TextWatcher  textEditorWatcher = new SafeTextWatcher(); //custom
        final LayoutParams lparams = new LayoutParams(100,100);
        final EditText edittext= new EditTextPersist(mContext);

        edittext.addTextChangedListener(textEditorWatcher);
        edittext.setInputType(InputType.TYPE_CLASS_TEXT               | 
                              InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS |
                              InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS
                              );
        edittext.setLayoutParams(lparams);
        //edittext.setEms(10); //doesn't work either


        // =============================================================
        // ======== create alert dlg, set btn callbacks ================
        // =============================================================

        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        builder.setPositiveButton("Upgrade", new DialogInterface.OnClickListener() 
        {
          public void onClick(DialogInterface dialog, int whichButton) 
          {
            String activationKey = edittext.getText().toString();
            on_key_entered(activationKey);
          }
        } );
        builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
          public void onClick(DialogInterface dialog, int whichButton) 
          {
            post_activation_result(QlmLicense.ACTIVATION_INVALID);
          }
        } );


        // =============================================================
        // ======== configure the AlertDialog/EditText =================
        // =============================================================

        int nWidth, nHeight;
        final float scale = mContext.getResources().getDisplayMetrics().density;
        nWidth  = (int) (900 * scale + 0.5f);
        nHeight = (int) (500 * scale + 0.5f);

        builder.setView(edittext)
               .setMessage("Enter an Activation Key:")
               .setTitle("Upgrade From Trial Version")
               .setCancelable(false)
               .show()
               .getWindow()
               .setLayout(nWidth,nHeight);
    }

我对你的问题有一个简单的答案,即通过将 LayoutParams 更改为 FrameLayout.LayoutParams 并调用 edittext.setLayoutParams(lparams); 调用后 dialog.show():

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

int nWidth, nHeight;
final float scale = getApplicationContext().getResources().getDisplayMetrics().density;
nWidth  = (int) (900 * scale + 0.5f);
nHeight = (int) (500 * scale + 0.5f);

final FrameLayout.LayoutParams lparams = new FrameLayout.LayoutParams(100,100);
final EditText edittext= new EditText(this);

            //edittext.addTextChangedListener(textEditorWatcher);
edittext.setInputType(InputType.TYPE_CLASS_TEXT |
                            InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS |
                            InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS
);

            // =============================================================
            // ======== create alert dlg, set btn callbacks ================
            // =============================================================

builder.setPositiveButton("Upgrade", new DialogInterface.OnClickListener()
{
     public void onClick(DialogInterface dialog, int whichButton)
     {
           String activationKey = edittext.getText().toString();
                    //on_key_entered(activationKey);
           Toast.makeText(getApplicationContext(), activationKey, Toast.LENGTH_LONG).show();

       }
} );
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
                    //post_activation_result(QlmLicense.ACTIVATION_INVALID);
    }
});


builder.setView(edittext)
.setMessage("Enter an Activation Key:")
                        .setTitle("Upgrade From Trial Version")
                        .setCancelable(false)
                        .show()
                        .getWindow()
                        .setLayout(nWidth, nHeight);


//Change setLayoutParams to here       
edittext.setLayoutParams(lparams);