开发者

Android: Adding a EditText programatically when a button is pressed

I am working on an SMS application. I have a "+" button so that when user clicks that button a new ExitText will appear under the existing one for user to 开发者_JAVA技巧enter multiple phone numbers to send the text. Can anyone please help with creating a new EditText when a button is pressed?

Thank You,


I would keep a List of EditText objects and add a new one

EditText toAdd = new EditText(this);
list.add(toAdd);

to the list on button press. Also, read this link for how to add that new EditText to your current layout. How to lay out Views in RelativeLayout programmatically?

When you know the user is done and wants to save the numbers, iterate through your List of EditText objects.


I built an app that adds buttons dynamically based on how many rows there on in my database.

basically I found it easier to create an array of buttons with length equal to the number of buttons I need: In your case...

final int PHONE_NUMBERS = 0;

final int OTHER_STUFF = 1;

final int MORE_STUFF = 2;

LinearLayout MyEditTextLayout;

EditText []DynamicFields = new EditText[3];

*note these should be declared outside of onCreate*

then within onCreate {

MyEditTextLayout = (LinearLayout) findViewById (R.id.Whatever_you_named_your_layout_in_xml);

}

then in your onClickListener dialog:

final EditText editText = new EditText();

if(button = myPhoneNumberButton)
{

editText.layout_width = "fill_parent";

editText.hint = "Enter Phone Numbers Here";

DynamicFields[PHONE_NUMBERS] = editText; //that way you can refer to your editTexts later

MyEditTextLayout.addView(editText);

}

please note I typed this out quickly at work so the code might not work exactly as is but this should give you a good start Comment if you have any questions!


In order to create a dialog with an EditText inside, you can do this in the button's OnClickListener:

final FrameLayout fl = new FrameLayout(ContactView.this);
final EditText txtSms = new EditText(ContactView.this);
txtSms.setRawInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);
txtSms.setInputType(InputType.TYPE_TEXT_FLAG_CAP_SENTENCES);
txtSms.setHorizontallyScrolling(false);
txtSms.setVerticalScrollBarEnabled(true);
txtSms.setGravity(Gravity.CENTER);
fl.addView(txtSms, new FrameLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT));

    final AlertDialog.Builder builder = new AlertDialog.Builder(ContactView.this);
        //building the custom AlertDialog
        builder.setView(fl).setTitle("Enviar mensaje").setCancelable(false)
        .setPositiveButton("Send", new DialogInterface.OnClickListener(){

                    //What happens when the positive button is pressed
            public void onClick(DialogInterface d, int which){
                if(txtSms.getText().toString().length() != 0){
                    enviarSms(txtSms.getText().toString());
                    d.dismiss();
                }else{
                    Toast.makeText(((Dialog) d).getContext(), "Can't send an empty message", Toast.LENGTH_SHORT).show();
                }
            }
                    //What happens when the negative button is pressed
        }).setNegativeButton("Cancel", new DialogInterface.OnClickListener(){
                public void onClick(DialogInterface d, int which) {
                d.dismiss();
            }
        }).create().show();
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜