添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
温柔的砖头  ·  java socketchannel ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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.

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.

Heads Up: alertDialog.setView(View, int, int, int, int); does not exist in the new android api versions. – David Passmore Feb 14, 2015 at 12:21 Builder.setView can only be called from within the same library group (groupId=androidx.appcompat) Inspection info:This API has been flagged with a restriction that has not been met. – behelit Mar 8, 2019 at 3:49
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.

Thanks for the heads up on the deprecation of SingleLine. They don't mention it everywhere in the documentation. Still, it doesn't seem like I should have to pick some number of pixels to correct this. I am assuming that the present result is incorrect because of my misuse of AlertDialog.Builder (or perhaps it is entirely the wrong technique). – altendky Feb 7, 2011 at 23:16 I'm not exactly sure how to apply that... but despite sp not being px, it would still be me arbitrarily setting some spacing rather than letting android use a default spacing. I don't want to adjust the layout to my personal preference, I want Android to do it to it's preference. – altendky Feb 8, 2011 at 2:25 Padding applies to things inside of the view so EditText.setPadding() creates space around the text inside of the EditText. I did try ((View) input.getParent()).setPadding(10, 10, 10, 10) after builder.setView(input), but this resulted in a null pointer exception. Regardless, I don't want to find a way to customize it to my personal preference of 5px or such, I want to allow the SDK to format it correctly. I assume I am using the SDK incorrectly and thus the formatting doesn't look right. – altendky Feb 7, 2011 at 22:34

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.