gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 05:22:10 24 4
gpt4 key购买 nike

好吧,我想获取 Edittext 的内容,以便将此文本设置为按钮的标签。我的问题是,Edittext 位于我想使用的另一种布局中。所以我有一个名为“Collection.java”的类,它引用名为“activity_collection.xml”的布局。在这个类中,我想获取 Edittext 的内容。还有一个名为“OpenProject.java”的其他类,其中包含一个对话框,其中包含 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 布局 View 中找到您的 EditText

关于java - 如何从另一个布局获取Edittext的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24259800/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com