好吧,这可能有点简单,但我不知道该怎么做。如何更改此函数以返回 String class_name
?我知道我需要将函数从 void
更改为 String
,但我还需要做什么?
非常感谢!
public void addClass() {
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Add Class");
final EditText input = new EditText(this);
alert.setView(input);
alert.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String class_name = input.getText().toString();
}
});
alert.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
});
alert.show();
}
最佳答案
返回 class_name
值没有帮助。您必须实现一个回调方法来获取值并执行所需的操作:
public void addClass() {
AlertDialog.Builder alert = new AlertDialog.Builder(this);
// ...
alert.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
setClassName(input.getText().toString());
}
});
// ...
}
protected void setClassName(String class_name) {
// do what ever has to be done with class_name
}
关于java - 通过多个函数返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3624091/