简体   繁体   中英

How to get content of Edittext from another layout

Well i want to get the content of an Edittext, in order to set this text as label for Buttons. My problem is, that the Edittext is in another layout that I want to use it. So I have got a Class called "Collection.java" which is referred to the layout called "activity_collection.xml". In this Class I want to get the content of the Edittext. And there is an other Class called "OpenProject.java" which contain an Dialogbox, where the Edittext is.

So I want to get the content from "Collection.java":

private void onaddButtonClick() {

    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
            context);

    LayoutInflater inflater = thisActivity.getLayoutInflater();

    alertDialogBuilder
            //zeigt den Inhalt von dialogbox.xml an
            .setView(inflater.inflate(R.layout.dialogbox, null))    
            .setCancelable(false)

            // OK button der Dialogbox hinzufügen
            .setPositiveButton(R.string.ok,
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {

                            setContentView(R.layout.dialogbox);
                            EditText text = (EditText) findViewById( R.id.projectname2);
                            Log.d("text", "text " + text.getText().toString());
                            // TODO: Editfeld auslesen (Problem: verschiedene Layout Dateien)
                            // newProjectButton( ) wird aufgerufen, wenn OK Button gedrückt wurde
                            newProjectButton(dialog, id);
                            dbHelper.addProject("Bitte Implementieren"); // TODO DO IT

                            drawAllProjects("B");
                        }
                    })

            // Abbrechen Button der Dialogbox hinzufügen
            .setNegativeButton(R.string.cancel,
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {

                            // zurückkehren zur aktuellen View
                            dialog.cancel();
                        }
                    });


    // Das Dialogfeld erstellen
    AlertDialog alertDialog = alertDialogBuilder.create();

    // das Dialogfeld anzeigen lassen
    alertDialog.show();

}

As you can see, I tried it with:

setContentView(R.layout.dialogbox);
EditText text = (EditText) findViewById( R.id.projectname2);
Log.d("text", "text " + text.getText().toString());

But everytime when I type something in, the content is empty.

What can I do, to get the content of the Edittext?

Try this way:

View v=inflater.inflate(R.layout.dialogbox, null);
alertDialogBuilder
        //zeigt den Inhalt von dialogbox.xml an
        .setView(v)    
      .....
    ......
  EditText text = (EditText) v.findViewById( R.id.projectname2);
  Log.d("text", "text " + text.getText().toString());

find your EditText from inflated layout view.

Get the Value of EditText in the Java class of the Layout containing the EditText. Now use Intent Extras to Pass the content to the Other Layout. This might come in Handy http://www.vogella.com/tutorials/AndroidIntent/article.html

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