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

时间:2015-08-20 19:18:04

标签: android android-edittext alertdialog android-alertdialog

问题:

  • 如何通过LayoutParams限制AlertDialog中包含的EditText的宽度?

假设

  • 我有一个AlertDialog,其宽度设置得相当大;
  • 在此对话框中有一个EditText,其宽度应为其容器的1/3;

次要详情

  • EditText具有以下类型:

    • TYPE_CLASS_TEXT
    • TYPE_TEXT_FLAG_CAP_CHARACTERS
    • TYPE_TEXT_FLAG_NO_SUGGESTIONS
  • AlertDialog的高度/宽度以像素为单位,通过密度缩放(请参阅代码);

困惑:

  • 鉴于以下代码,为什么EditText最终会跨越AlertDialog的整个宽度?

守则

  • 请注意,EditText通过LayoutParams设置为{100,100},而AlertDialog设置为{900,500}
    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);
    }

1 个答案:

答案 0 :(得分:3)

我的问题很简单,只需将LayoutParams Edittext AlertDialog LayoutParams改为FrameLayout.LayoutParamsedittext.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); 致电ccInner

isInIframe