2017-02-20 72 views
3

有沒有辦法在AlertDialog的Android創建多的EditText。我設置了setLines,並且它顯示了幾行較大的EditText。但是當我輸入時,它不會進入下一行,並且仍然鍵入相同行。這是我的代碼。的Android AlertDialog多的EditText

Builder builder = new AlertDialog.Builder(this); 
     builder.setTitle("Comment"); 

     final EditText input = new EditText(this); 

     final String item_value = ItemList.get(position).get("comment"); 

     input.setText(item_value); 
     input.setInputType(InputType.TYPE_CLASS_TEXT); 
     input.setLines(5); 
     input.setMaxLines(5); 
     input.setGravity(Gravity.LEFT | Gravity.TOP); 
     builder.setView(input); 

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


      } 
     }); 

     builder.setNegativeButton("Cancel", 
       new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         dialog.dismiss(); 
        } 
       }); 
     AlertDialog alert = builder.create(); 
     alert.show(); 

我的對話框如下所示。 enter image description here

那麼我該如何解決這個問題。感謝致敬。

回答

4

嘗試此代碼爲您的EditText:

input.setSingleLine(false); //add this 
input.setLines(4); 
input.setMaxLines(5); 
input.setGravity(Gravity.LEFT | Gravity.TOP); 
input.setHorizontalScrollBarEnabled(false); //this 
-1

在XML中設置屬性android:inputType="textMultiLine"

如果不起作用,您必須執行TextWatcher並自行手動分行。

3

使用輸入類型input.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE)input.setSingleLine(false)。所以你的代碼將是 -

Builder builder = new AlertDialog.Builder(this); 
     builder.setTitle("Comment"); 

     final EditText input = new EditText(this); 

     final String item_value = ItemList.get(position).get("comment"); 

     input.setText(item_value); 
     input.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE); 
     input.setSingleLine(false) 
     input.setLines(5); 
     input.setMaxLines(5); 
     input.setGravity(Gravity.LEFT | Gravity.TOP); 
     builder.setView(input); 

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


      } 
     }); 

     builder.setNegativeButton("Cancel", 
       new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         dialog.dismiss(); 
        } 
       }); 
     AlertDialog alert = builder.create(); 
     alert.show(); 
2

這是爲我工作。您可以使用此屬性爲您運行時EditText。

<EditText 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:gravity="top|left" 
     android:inputType="textMultiLine" 
     android:lines="8" 
     android:maxLines="10" 
     android:minLines="6" 
     android:scrollbars="vertical" /> 
1
Builder builder = new AlertDialog.Builder(this); 
builder.setTitle("Comment"); 

final EditText input = new EditText(this); 

final String item_value = ItemList.get(position).get("comment"); 

input.setText(item_value); 
input.setInputType(InputType.TYPE_CLASS_TEXT); 
input.setSingleLine(false); 
input.setMaxLines(5); 
input.setGravity(Gravity.LEFT | Gravity.TOP); 
builder.setView(input); 

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

    } 
}); 

builder.setNegativeButton("Cancel",new DialogInterface.OnClickListener() { 
@Override 
public void onClick(DialogInterface dialog, int which) { 
    dialog.dismiss(); 
    } 
}); 
AlertDialog alert = builder.create(); 
alert.show();