alerttdialog выглядит нормально, но, возможно, ошибка находится в остальной части кода.Вы должны иметь в виду, что вы не можете использовать var playerName, а просто показывает show () диалогового окна. Если вы хотите распечатать имя, вы должны сделать это с исполняемым файлом, который вы вызываете здесь:
static Handler handler = new Handler();
[.......]
public void onClick(DialogInterface dialog, int whichButton) {
playerName = input.getText().toString();
handler.post(set_playername);
}
[.......]
static Runnable set_playername = new Runnable(){
@Override
public void run() {
//printout your variable playerName wherever you want
}
};
изменить, чтобы уточнить:
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setMessage("Your Name");
final EditText input = new EditText(this);
alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
playerName = input.getText().toString();
//call a unction/void which is using the public var playerName
}
});
alert.show();
// the variable playerName is NULL at this point