使用布局参数限制 AlertDialog 中 EditText 的宽度?
Limit the width of an EditText within an AlertDialog with Layout Params?
问题:
- 我们如何通过 LayoutParams 限制包含在 AlertDialog 中的 EditText 的宽度?
假设:
- 我有一个AlertDialog,它的宽度设置得比较大;
- 在此对话框中有一个 EditText,其宽度应为其容器的 ~1/3;
次要细节:
EditText有以下几种类型:
- TYPE_CLASS_TEXT
- TYPE_TEXT_FLAG_CAP_CHARACTERS
- TYPE_TEXT_FLAG_NO_SUGGESTIONS
AlertDialog的height/width以像素为单位设置,通过密度缩放(见代码);
困惑:
- 给定以下代码,为什么 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);
}
我对你的问题有一个简单的答案,即通过将 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);
问题:
- 我们如何通过 LayoutParams 限制包含在 AlertDialog 中的 EditText 的宽度?
假设:
- 我有一个AlertDialog,它的宽度设置得比较大;
- 在此对话框中有一个 EditText,其宽度应为其容器的 ~1/3;
次要细节:
EditText有以下几种类型:
- TYPE_CLASS_TEXT
- TYPE_TEXT_FLAG_CAP_CHARACTERS
- TYPE_TEXT_FLAG_NO_SUGGESTIONS
AlertDialog的height/width以像素为单位设置,通过密度缩放(见代码);
困惑:
- 给定以下代码,为什么 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);
}
我对你的问题有一个简单的答案,即通过将 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);