簡體   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