2011-03-18 53 views
1

我想創建一個類似於這個圖的AlertDialog。我無法調整EditText視圖的大小。我如何調整它?調整Android中的Alert對話框中的編輯框

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(); 
+0

你可以發佈你的代碼嗎? – 2011-03-18 10:25:30

+0

你的代碼發生了什麼? – 2011-03-18 10:43:09

回答

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

請根據您的需求更改線性佈局填充。

+0

謝謝你Chirag ..它按預期工作:) – indira 2011-03-18 10:49:47

+0

你歡迎...... – 2011-03-18 10:52:05

+0

夥計們。如果我在LinearLaytou上設置Button,如何在單擊Button時關閉/解除AlerDialog? – Ferdinand 2011-04-15 00:07:17

-1

而不是創建警報對話框,你可以充氣視圖,並保持對自己的高度和寬度和兩個按鈕確定,取消的編輯文本框的..

檢查此爲充氣一個視圖Inflate View .....