【问题标题】:Limit the width of an EditText within an AlertDialog with Layout Params?使用布局参数限制 AlertDialog 中 EditText 的宽度?
【发布时间】:2015-09-08 17:42:38
【问题描述】:

问题:

  • 我们如何通过 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);
    }

【问题讨论】:

  • 在为layout 添加dialog 的视图之前,尝试将您的edittext 设置为LinearLayout 并为此margin 设置layout。希望这个答案可以帮助你:stackoverflow.com/questions/9345735/…
  • 啊,是的。非常感谢。这回答了限制 AlertDialog 宽度的“如何完成”的问题。一个更有用和直接的问题是“为什么 LayoutParams 在应用于 AlertDialog 中的 EditText 时不起作用”。我会进行编辑!感谢您提供方法:)
  • 鉴于更具体的问题,如果您找到一种将您找到的解决方案与使用 LayoutParams 相结合的方法,如果您发布一个答案,我将很乐意接受您的答案!
  • 如果它可以帮助你很好。我现在不在家,所以我回家时会发布答案^^。谢谢你:)

标签: android android-edittext android-alertdialog


【解决方案1】:

对于您的问题,我有一个简单的答案,即通过将LayoutParams 更改为FrameLayout.LayoutParams 并在调用dialog.show() 后调用edittext.setLayoutParams(lparams); 来将Edittext 应用于AlertDialog 中的Edittext

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);

【讨论】:

    猜你喜欢
    • 2016-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-01
    • 2011-09-01
    • 2015-10-02
    • 1970-01-01
    • 2011-06-23
    相关资源
    最近更新 更多