Web開発のセッションのようなコンテキストに変数を設定できますか?
Androidアプリケーションが開始されるとすぐに確認ボックスを開発している私のコードは次のとおりです。
package com.example.alertboxandloadingwidgets;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.Menu;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Boolean result = showConfirmationBox("Are you sure you want to do this",
this);
}
public Boolean showConfirmationBox(String messageToShow, final Context context) {
// prepare the alert box
AlertDialog.Builder alertbox = new AlertDialog.Builder(context);
// set the message to display
alertbox.setMessage(messageToShow);
// set a positive/yes button and create a listener
alertbox.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
// do something when the button is clicked
public void onClick(DialogInterface arg0, int arg1) {
Toast.makeText(context,
"'Yes' button clicked", Toast.LENGTH_SHORT)
.show();
}
});
// set a negative/no button and create a listener
alertbox.setNegativeButton("No", new DialogInterface.OnClickListener() {
// do something when the button is clicked
public void onClick(DialogInterface arg0, int arg1) {
Toast.makeText(context, "'No' button clicked",
Toast.LENGTH_SHORT).show();
}
});
// display box
alertbox.show();
}
}
しかし、ボタンがクリックされた場合yes
は戻る必要がtrue
あり、no
ボタンがクリックされた場合は戻る必要がありますfalse
。
onClickListener
ただ、返品タイプが無効になっているのでできません。
アップデート
しかし、問題は、私がそれを一般的な手段にしていることです。このメソッドは、どのアクティビティでもこのメソッドを使用できるCommonUtilitiesクラスに書き込む必要があります。したがって、このメソッドを呼び出している場所から結果パラメーターの値を設定またはリセットする必要があります。