2

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クラスに書き込む必要があります。したがって、このメソッドを呼び出している場所から結果パラメーターの値を設定またはリセットする必要があります。

4

9 に答える 9

7

Androidダイアログは非同期であるため、これに対処するにはコードをリファクタリングする必要があります。私はあなたがこのようなことをすることを計画していたと思います:

boolean result = showConfirmation(...);
if(result) {
    //do something
}
else {
    //do something else
}

次のようなもので同じ結果を得ることができます:

public class MainActivity extends Activity {
    private boolean result;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        showConfirmationBox("Are you sure you want to do this", this);
    }

    private doOnTrueResult() {
        result = true;
        //do something
    }

    private doOnFalseResult() {
        result = false;
        //do something else
    }

    public void 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();
                doOnTrueResult();
            }
        });

        // 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();
                doOnFalseResult();
            }
        });

        // display box
        alertbox.show();
    }
}
于 2013-03-21T10:15:16.663 に答える
3

これは私が常にダイアログボックスからのデータを処理する方法です

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();
               myFunction(item);
        }
    });

private void myFunction(int result){
// Now the data has been "returned" (that's not
// the right terminology)
}

同様に、他のボタンには別の機能を使用します

于 2013-03-21T10:14:31.243 に答える
1

onClickListenerからの値をグローバル変数または別のメソッドに渡す必要があります。正しく認識しているので、の戻りタイプonClickListenervoidです。より複雑な解決策については、この投稿をご覧ください

于 2013-03-21T10:12:49.670 に答える
1

結果値のを作成setterし、その値をメソッドで選択した値に変更しますonClick()

showConfirmationBox無効にする;-)

于 2013-03-21T10:13:31.297 に答える
1

関数の場合

public Boolean showConfirmationBox(String messageToShow, final Context context)

メインスレッドで呼び出す必要がありますが、それはできません。メインスレッドでのユーザー入力を待つことはありません。それはANRを引き起こします。

関数をバックグラウンドスレッドで呼び出すことができる場合は、メインスレッドにメッセージを送信してアラートボックスを表示し、結果を待つことができます。「ハンドラー」を上手に活用してください。

于 2013-03-21T10:14:01.347 に答える
1

それはできませんが、uはブール変数を作成し、yesの場合はtrueを、noの場合はFalseを格納できます。その後、uはその変数を適宜使用できます。

于 2013-03-21T10:14:26.080 に答える
1

あなたがそれをすることができる1つの簡単な方法:

public class MainActivity extends Activity {
    public static boolean result;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        showConfirmationBox("Are you sure you want to do this", this);

    }

    public Boolean showConfirmationBox(String messageToShow, final Context context) {
        AlertDialog.Builder alertbox = new AlertDialog.Builder(context);
        alertbox.setMessage(messageToShow);
        alertbox.setPositiveButton("Yes",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface arg0, int arg1) {
                Toast.makeText(context, "'Yes' button clicked", Toast.LENGTH_SHORT).show();
                MainActivity.result = true;
            }
        });

    // 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();
            MainActivity.result = false;
        }
    });

    // display box
    alertbox.show();

    }
}
于 2013-03-21T10:15:17.247 に答える
0

オプションの1つは、

public Button getButton (int whichButton)
Gets one of the buttons used in the dialog.

this Returns
The button from the dialog, or null if a button does not exist.

詳細については、リンクhttp://developer.android.com/reference/android/app/AlertDialog.htmlを確認してください。

于 2013-03-21T10:20:23.210 に答える
0

これはあなたを助けるかもしれません

public class MainActivity extends Activity {
     Boolean mresult;

    @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);
          Toast.makeText(getApplicationContext(), ""+result, Toast.LENGTH_LONG).show();


    }

     public Boolean showConfirmationBox(String messageToShow, final Context context) {       

            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();

                    mresult = true;
                }
            });
            // 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();

                    mresult = false;
                }
            });
            // display box
            alertbox.show();
            return mresult;
        }


}
于 2013-03-21T10:39:06.790 に答える