简体   繁体   中英

Add new text item into list view on Android

I have an Activity with the ListView with text items inside. When user presses the "Add new" menu button he can add the new item into the ListView by typing a new string.

What is the best way of displaying some text input widget? Creating a dialog or another activity? How is this usually handled on Android.

Thanks a lot,

Regards

STEN

You would probably want an AlertDialog with an EditText widget in it. You can also add buttons for "OK" and "Cancel" and take in the value in the EditText when the user clicks OK.

Simply you should use a Custom Dialog with an EditText field, you don't need to create an Activity for this. It will results in more complexity.

If you really need it using a different Activity then you can try the Notepad Tutorial . It uses same thing to add new Notes.

call this methode when you select "Add New"

void addNew()
{

       Context context = MyActivity.this;
       AlertDialog.Builder alert = new AlertDialog.Builder(context);

        alert.setTitle("NEW TITLE");
        alert.setMessage("MESSAGE 1");

         // Set an EditText view to get user input   
          final EditText input = new EditText(this); 
          alert.setView(input);


        alert.setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {

                /* User clicked OK so do some stuff */
            }
        });

        alert.setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {

                /* User clicked Cancel so do some stuff */
            }
        });

        alert.create();
        alert.show();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM