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)
}

同样,对其他 Button 使用另一个函数

于 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

你不能这样做,但你可以创建一个布尔变量,如果是,则存储 true,如果不是,则存储 False,然后你可以相应地使用该变量

于 2013-03-21T10:14:26.080 回答
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

one of the option would be using the

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.

for more information check the link 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 回答