在android中的对话框中设置多个文本框

我正在创build一个对话框,就像一个包含两个文本框和两个button的login屏幕。 我能够创build它,但我的问题是两个编辑文本框相互重叠(第二个编辑文本框与第一个重叠)。 这可能是一个简单的,但因为即时通讯新的Android即时通讯坚持它,请帮我解决它。 这里是源代码

public class LoginActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button btn= (Button) findViewById(R.id.btn_Login); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { showDialog(0); } }); } protected Dialog onCreateDialog(int id) { final AlertDialog.Builder alert = new AlertDialog.Builder(this); final EditText input = new EditText(this); final EditText input1 = new EditText(this); alert.setIcon(R.drawable.icon); alert.setTitle("Login"); alert.setView(input); alert.setView(input1); alert.setView(input1); alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { String value = input.getText().toString().trim(); Toast.makeText(getApplicationContext(), value, Toast.LENGTH_SHORT).show(); } }); alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { dialog.cancel(); } }); return alert.create(); } } 

Solutions Collecting From Web of "在android中的对话框中设置多个文本框"

您也可以为对话框创build布局XML。 在调用你的对话框之前,只需要:

 myDialog.setContentView(R.layout.my_dialog_layout); 

如果您尝试使用LinerLayout

 LinearLayout lila1= new LinearLayout(this); lila1.setOrientation(LinearLayout.VERTICAL); final EditText input = new EditText(this); final EditText input1 = new EditText(this); lila1.addView(input); lila1.addView(input1); alert.setView(lila1); 

喜欢这个:

 public class LoginActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button btn= (Button) findViewById(R.id.btn_Login); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { showDialog(0); } }); } protected Dialog onCreateDialog(int id) { final AlertDialog.Builder alert = new AlertDialog.Builder(this); LinearLayout lila1= new LinearLayout(this); lila1.setOrientation(1); //1 is for vertical orientation final EditText input = new EditText(this); final EditText input1 = new EditText(this); lila1.addView(input); lila1.addView(input1); alert.setView(lila1); alert.setIcon(R.drawable.icon); alert.setTitle("Login"); alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { String value = input.getText().toString().trim(); Toast.makeText(getApplicationContext(), value, Toast.LENGTH_SHORT).show(); } }); alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { dialog.cancel(); } }); return alert.create(); } } 

它完美的作品:我邀请你复制粘贴:)。

thnx男人,这是工作! 但我在这种情况下,它给了我一个错误,所以我改变这一点:

 lila1.setOrientation(1); //1 is for vertical orientation 

对此:

 lila1.setOrientation(LinearLayout.VERTICAL);