在Android中的“警报”对话框中调整“编辑”框

时间:2011-03-18 10:22:09

标签: android

我想创建一个类似于这个数字的AlertDialog。我无法调整EditText视图的大小。我该如何调整大小?

enter image description here

alert = new AlertDialog.Builder(this);
alert.setTitle("Enter Destination");
alert.setIcon(R.drawable.icon);

final EditText editAddress = new EditText(this);
editAddress.setLayoutParams(new LayoutParams(200, 20)); 
alert.setView(editAddress);

alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int id) {
    }
});

alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int id) {
    }
});

alert.show();   

2 个答案:

答案 0 :(得分:3)

这里我添加了一个将打开对话框的函数。

 public void openDialog()
 {
     LinearLayout linearLayout = new LinearLayout(this);
     linearLayout.setLayoutParams(new LayoutParams(
                LayoutParams.FILL_PARENT,
                LayoutParams.FILL_PARENT));
     linearLayout.setPadding(30, 0, 30, 0);

    final EditText saveas = new EditText(this);
    saveas.setLayoutParams(new LayoutParams(
            LayoutParams.FILL_PARENT,
            LayoutParams.FILL_PARENT));
    saveas.setImeOptions(EditorInfo.IME_ACTION_DONE);
    saveas.setInputType(InputType.TYPE_TEXT_FLAG_CAP_SENTENCES);
    saveas.setHint("Folder");
    AlertDialog.Builder dialog = new AlertDialog.Builder(this);
    dialog.setTitle("Rename Folder");
    dialog.setMessage("Rename Folder");

    linearLayout.addView(saveas);

    dialog.setView(linearLayout);

    dialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() 
    {
        public void onClick(DialogInterface dialoginterface, int buttons) 
        {

        }
    });

    dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() 
    {
        public void onClick(DialogInterface arg0, int buttons) 
        {
        }
    });
    dialog.show();
 }

请根据您的要求更改线性布局填充。

答案 1 :(得分:-1)

您可以对视图进行充气并保持编辑自己的高度和宽度的文本框,而不是创建警报对话框,然后按两个按钮,取消..

检查这是否夸大了视图Inflate View .....