alertdialogは、ハンドラーなしで返品値を取得します
-
30-09-2019 - |
質問
多くのalertdialogsを見せたいので、ユーザーはいくつかの質問を処理する必要があります(ウィザードのように少し)。
ユーザーが何かを選択してから選択した値を返すまで、alertdialogを待つことは可能ですか?
HashMap<Integer, Question> questions = DataConnector.getCallQuestions(position);
int nextQuestion = position;
while(nextQuestion != 0){
Question question = questions.get(nextQuestion);
CharSequence[] items = (String[])question.getAnswers();
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(question.getQuestion());
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
}
});
AlertDialog alert = builder.create();
//I would like to do something like this:
nextQuestion = alert.getClickedItem();
}
編集. 。 Chrisに反対する:
プログラムの実行は、ユーザーがalertdialogでオプションの1つを選択するまで待つ必要があります
これは可能ですか?
このような:
private int answer = 0;
....
...methode(){
//show dialog
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(question.getQuestion());
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
CallHandleMenu.this.answer = item; //setting answer
}
});
builder.create().show();
//Here I need to know the answer (the answer should not be 0 here)
解決 2
私のコードが汚れているので、私はそれをこの方法で修正しました(pseudocode)
activity{
onCreate{
makeNextQuestion(1)
}
public void nextQuestion(int questionId){
//add string and answering buttons to layout
btn.onClickList(){
onClick(View btn){
activity.this.nextQuestion(btn.getId());
}
}
}
}
他のヒント
一連のシリーズが必要な場合 AlertDialog
表示されるボックスでは、最善の策は、ダイアログ自体にリスナーを設定することです。
.setPositiveButton("My Button Name", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//do stuff like set some variables, maybe display another dialog
}
})
アラートダイアログにデフォルトの正のボタンを使用していることに注意してください。ユーザーがクリックしているものに応じて、これを変更する必要がある場合があります。
あなたがあなたにいくつかのコールバックをするつもりなら Activity
からのクラス AlertDialog
onClickListener
使用できます MyActivityClassName.this.myMethodOrVariableHere
そうするために。
それが役立つことを願っています、あなたがもう少し明確にしたいなら、私にコメントを残してください。
所属していません StackOverflow