开发者

Set multiple text boxes in a dialog in android

I am creating a dialog box which is like a login screen containing two text boxes and two buttons. I am able to create it but my problem is the two edit text boxes are overlapping with each other(Second edit text box is overlapping with the first one). It may be a simple one but since i m new to android i m stuck with it.Pls help me to solve it. Here is the source code

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 whichB开发者_开发百科utton) {          
                dialog.cancel();    }     });         
        return alert.create();      
        } 
    }


What if you try with a 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);

Like this:

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

And it works perfectly : I invite you to copy paste it :).


You can create a layout XML for dialog boxes as well. Before calling your Dialog just do:

myDialog.setContentView(R.layout.my_dialog_layout);


thnx men, it is work!!! but i in this case it give me an errror, so i change this:

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

on this:

lila1.setOrientation(LinearLayout.VERTICAL);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜