AlertDialog 中的 Android EditText 似乎太宽

发布于 2024-10-16 02:15:12 字数 1381 浏览 3 评论 0原文

下图中的 EditText 似乎太宽了。我假设我以某种方式滥用了 SDK,除非确信,否则我不会寻找一种方法来指定 EditText 两侧的一定数量的边距/填充像素。

在此处输入图像描述

这个看起来更合适。

在此处输入图像描述

这是我的代码(创建第一个“创建标签”对话框):

final Dao<Tag, Integer> tagDao = getHelper().getTagDao();

final EditText input = new EditText(this);
input.setSingleLine(true);
input.setHint(R.string.create_tag_dialog_hint);

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(input);
builder.setTitle(getString(R.string.create_tag_dialog_title));
builder.setPositiveButton(
    getString(R.string.create_tag_dialog_positive),
    new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            String value = input.getText().toString().trim();
            Toast.makeText(getApplicationContext(), value, Toast.LENGTH_SHORT).show();
            Tag tag = new Tag(value);
            try {
                    tagDao.create(tag);
            } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
            }
        }
    });
builder.setNegativeButton(
    getString(R.string.create_tag_dialog_negative), null);
builder.show();

抱歉,帖子并感谢任何有用的评论。

It seems like the EditText in the image below is too wide. I assume that I have misused the SDK in some way and until convinced otherwise I am not looking for a way to specify some number of margin/padding pixels on the sides of the EditText.

enter image description here

This one looks more appropriate.

enter image description here

Here's my code (that creates the first, 'Create Tag', dialog):

final Dao<Tag, Integer> tagDao = getHelper().getTagDao();

final EditText input = new EditText(this);
input.setSingleLine(true);
input.setHint(R.string.create_tag_dialog_hint);

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(input);
builder.setTitle(getString(R.string.create_tag_dialog_title));
builder.setPositiveButton(
    getString(R.string.create_tag_dialog_positive),
    new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            String value = input.getText().toString().trim();
            Toast.makeText(getApplicationContext(), value, Toast.LENGTH_SHORT).show();
            Tag tag = new Tag(value);
            try {
                    tagDao.create(tag);
            } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
            }
        }
    });
builder.setNegativeButton(
    getString(R.string.create_tag_dialog_negative), null);
builder.show();

Sorry for the length of the post and thanks for any helpful comments.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

故事还在继续 2024-10-23 02:15:12

我自己刚刚整理了这个。使用 AlertDialog 实例,您可以指定 setView 并传入间距参数。这会起作用。

final EditText input = new EditText(this);

AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("Title");
alertDialog.setMessage("Message");
alertDialog.setView(input, 10, 0, 10, 0); // 10 spacing, left and right
alertDialog.setButton("OK", new OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        // Clicked
    }
});
alertDialog.show();

编辑:我知道这个问题很旧,但没有提供解决方案。

Just sorted this myself. Using an instance of AlertDialog, you can specify setView and pass in spacing parameters. This will work.

final EditText input = new EditText(this);

AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("Title");
alertDialog.setMessage("Message");
alertDialog.setView(input, 10, 0, 10, 0); // 10 spacing, left and right
alertDialog.setButton("OK", new OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        // Clicked
    }
});
alertDialog.show();

Edit: I'm aware this question is old, but no solution was provided.

音盲 2024-10-23 02:15:12

您可以这样做:

LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setGravity(Gravity.CENTER_HORIZONTAL);
final EditText input = new EditText(this);
input.setSingleLine(true);
layout.setPadding(10, 0, 10, 0);
input.setHint("Hint");
layout.addView(input);

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

此外,setSingleLine 已被弃用。您应该使用InputStyle

You can do it like this:

LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setGravity(Gravity.CENTER_HORIZONTAL);
final EditText input = new EditText(this);
input.setSingleLine(true);
layout.setPadding(10, 0, 10, 0);
input.setHint("Hint");
layout.addView(input);

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

Moreover, setSingleLine is deprecated. You should use InputStyle.

在梵高的星空下 2024-10-23 02:15:12

将布局 marginleft 和布局 marginright 设置为 5sp。边距设置视图组周围的空间。看看 ViewGroup.MarginLayoutParams

Set the layout marginleft and layout marginright to 5sp. The margin sets the space around the view group. Take a look at ViewGroup.MarginLayoutParams

饭团 2024-10-23 02:15:12

尝试使用 setPadding 方法。这应该在 EditText 的边缘和对话框的边框之间添加一个间隙。氮

Try setting some padding on your EditText using the setPadding method. This should add a gap between the edge of the EditText and the border of the dialog. N

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文