简体   繁体   English

如何从另一个布局获取Edittext的内容

[英]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. 好吧,我想获取Edittext的内容,以便将此文本设置为按钮的标签。 My problem is, that the Edittext is in another layout that I want to use it. 我的问题是,Edittext是另一种我想要使用它的布局。 So I have got a Class called "Collection.java" which is referred to the layout called "activity_collection.xml". 所以我有一个名为“Collection.java”的类,它被称为“activity_collection.xml”的布局。 In this Class I want to get the content of the Edittext. 在这个类中,我想获取Edittext的内容。 And there is an other Class called "OpenProject.java" which contain an Dialogbox, where the Edittext is. 还有一个名为“OpenProject.java”的类包含一个Dialogbox,其中Edittext是。

So I want to get the content from "Collection.java": 所以我想从“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? 我该怎么做才能获得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. inflated布局视图中找到您的EditText

Get the Value of EditText in the Java class of the Layout containing the EditText. 在包含EditText的Layout的Java类中获取EditText的值。 Now use Intent Extras to Pass the content to the Other Layout. 现在使用Intent Extras将内容传递给其他布局。 This might come in Handy http://www.vogella.com/tutorials/AndroidIntent/article.html 这可能来自Handy http://www.vogella.com/tutorials/AndroidIntent/article.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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