0
私は4つのEditTextでダイアログを開くアクティビティを持っています。すべてのフィールドを入力するにはどうすればよいですか? ?ボタンを押した後にダイアログを閉じる方法を教えてください
編集 ユーザーが戻るボタン を押した場合でも、これが私のコードです:
private void showEnterNamesDialog(final boolean edit){
final AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
LayoutInflater inflater = this.getLayoutInflater();
final View dialogView = inflater.inflate(R.layout.enter_names_dialog, null);
dialogBuilder.setView(dialogView);
final EditText[] plyrs_et = new EditText[4];
plyrs_et[0] = (EditText)dialogView.findViewById(R.id.plyr1_et_dialog);
plyrs_et[1] = (EditText)dialogView.findViewById(R.id.plyr2_et_dialog);
plyrs_et[2] = (EditText)dialogView.findViewById(R.id.plyr3_et_dialog);
plyrs_et[3] = (EditText)dialogView.findViewById(R.id.plyr4_et_dialog);
if(edit){
String[] names = gameList.getNames();
for (int i = 0; i < 4; i++){
plyrs_et[i].setText(names[i]);
}
dialogBuilder.setNegativeButton(getResources().getString(R.string.cancel), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
}
dialogBuilder.setTitle(getResources().getString(R.string.enter_players_names));
dialogBuilder.setPositiveButton(getResources().getString(R.string.submit_btn), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
String[] names = new String[4];
for(int j = 0; j < 4 ; j++){
names[j] = plyrs_et[j].getText().toString();
}
if(names[0].isEmpty() || names[1].isEmpty() || names[2].isEmpty() || names[3].isEmpty()) {
Toast error = Toast.makeText(getApplicationContext(), getResources().getString(R.string.toast_alert), Toast.LENGTH_SHORT);
error.show();
}else {
if(edit){
//set the names in textViews
hp1.setText(names[0]);
hp2.setText(names[1]);
hp3.setText(names[2]);
hp4.setText(names[3]);
gameList.setNames(names);
}else {
gameList.setNames(names);
setHeaderFooter(names);
listView.setAdapter(mAdapter);
}
}
}
});
AlertDialog b = dialogBuilder.create();
b.show();
}
このコードは、唯一のトーストメッセージを表示しますが、それでも、ダイアログを閉じます。 アイデア
http://stackoverflow.com/questions/2620444/how-to-prevent-a-dialog-from-closing-when -a-button-is-clicked – Darussian
私たちはポジティブボタンを上書きする必要があります...下の私の答えを参照してください。私は私のプロジェクトで同じことをしました –
@ダルシアンに感謝しますが、まだユーザーは戻るボタンを押すことができます –