「android開発」AlertDialogを利用してユーザーが選択したボタンを取得する方法
コード下記:
final ChoiceOnClickListener choiceListener = new ChoiceOnClickListener();
builder.setSingleChoiceItems(R.array.hobby, 0, choiceListener);
DialogInterface.OnClickListener btnListener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int which) {
int choiceWhich = choiceListener.getWhich();
String hobbyStr = getResources().getStringArray(R.array.hobby)[choiceWhich];
}
};
builder.setPositiveButton(“確認", btnListener);
private class ChoiceOnClickListener implements DialogInterface.OnClickListener {
private int which = 0;
@Override
public void onClick(DialogInterface dialogInterface, int which) {
this.which = which;
}
public int getWhich() {
return which;
}
}