繁体   English   中英

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

[英]How to get content of Edittext from another layout

好吧,我想获取Edittext的内容,以便将此文本设置为按钮的标签。 我的问题是,Edittext是另一种我想要使用它的布局。 所以我有一个名为“Collection.java”的类,它被称为“activity_collection.xml”的布局。 在这个类中,我想获取Edittext的内容。 还有一个名为“OpenProject.java”的类包含一个Dialogbox,其中Edittext是。

所以我想从“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();

}

如你所见,我尝试过:

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

但每当我输入内容时,内容都是空的。

我该怎么做才能获得Edittext的内容?

试试这种方式:

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

inflated布局视图中找到您的EditText

在包含EditText的Layout的Java类中获取EditText的值。 现在使用Intent Extras将内容传递给其他布局。 这可能来自Handy http://www.vogella.com/tutorials/AndroidIntent/article.html

暂无
暂无

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

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