如何使用警报构建器对话框显示消息

时间:2016-06-02 10:45:25

标签: java android

我想使用一个对话框来显示下面方法中包含的消息,而不仅仅是setText,但每次使用alertBuilder时,都不会显示消息。有人可以指出我正确的方向吗?

下面是代码:

             @Override

                public void onPreviewFrame(byte[] data, Camera cam) {

        if (data == null) throw new NullPointerException();
        Camera.Size size = cam.getParameters().getPreviewSize();
        if (size == null) throw new NullPointerException();

        if (!processing.compareAndSet(false, true)) return;

        int width = size.width;
        int height = size.height;

        int imgAvg = ImageProcessing.decodeYUV420SPtoRedAvg(data.clone(), height, width);
         Log.i(TAG, "imgAvg="+imgAvg);
        if (imgAvg == 0 || imgAvg == 255) {
            processing.set(false);
            return;
        }

        int averageArrayAvg = 0;
        int averageArrayCnt = 0;
        for (int i = 0; i < averageArray.length; i++) {
            if (averageArray[i] > 0) {
                averageArrayAvg += averageArray[i];
                averageArrayCnt++;
            }
        }

        int rollingAverage = (averageArrayCnt > 0) ? (averageArrayAvg / averageArrayCnt) : 0;
        TYPE newType = currentType;
        if (imgAvg < rollingAverage) {
            newType = TYPE.RED;
            if (newType != currentType) {
                beats++;
                // Log.d(TAG, "BEAT!! beats="+beats);
            }
        } else if (imgAvg > rollingAverage) {
            newType = TYPE.GREEN;
        }

        if (averageIndex == averageArraySize) averageIndex = 0;
        averageArray[averageIndex] = imgAvg;
        averageIndex++;

        // Transitioned from one state to another to the same
        if (newType != currentType) {
            currentType = newType;
            image.postInvalidate();
        }
           long endTime = 20;
                //System.currentTimeMillis();
        double totalTimeInSecs = (endTime - startTime) / 1000d;
        if (totalTimeInSecs >= 20) {
            double bps = (beats / totalTimeInSecs);
            int dpm = (int) (bps * 60d);
            if (dpm < 30 || dpm > 180) {
                startTime = System.currentTimeMillis();
                beats = 0;
                processing.set(false);
                return;
            }

             Log.d(TAG,
             "totalTimeInSecs="+totalTimeInSecs+" beats="+beats);


            if (beatsIndex == beatsArraySize) beatsIndex = 0;
            beatsArray[beatsIndex] = dpm;
            beatsIndex++;

            int beatsArrayAvg = 0;
            int beatsArrayCnt = 0;
            for (int i = 0; i < beatsArray.length; i++) {
                if (beatsArray[i] > 0) {
                    beatsArrayAvg += beatsArray[i];
                    beatsArrayCnt++;
                }
            }
            int beatsAvg = (beatsArrayAvg / beatsArrayCnt);
            //maybe here
            startTime = System.currentTimeMillis();
            beats = 0;

            text.setText(String.valueOf(beatsAvg) + "bpm");
        }

代码工作正常,但我只想使用对话框来显示text.setText消息。我该怎么做?

2 个答案:

答案 0 :(得分:3)

public class DialogCaller {

    public static void showDialog(Context context,String title,String message,
                                  DialogInterface.OnClickListener onClickListener) {

        AlertDialog.Builder dialog = new AlertDialog.Builder(context);
        dialog.setTitle(title);
        dialog.setMessage(message);
        dialog.setPositiveButton("Ok",onClickListener);
        dialog.show();
    }
}

将此类添加到您的android项目中。

每当你想要显示一条消息时,只需在对话框中调用具有上下文,标题,消息和监听器的类即可。像这样。

在text.setText(String.valueOf(beatsAvg)+“bpm”)的位置添加以下代码;

DialogCaller.showDialog(getActivity()/getApplicationContext(),"Kingmeka dialog ",String.valueOf(beatsAvg) + "bpm",new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                    dialog.dismiss();
                }
            });

答案 1 :(得分:-2)

      AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
      alertDialogBuilder.setMessage(String.valueOf(beatsAvg) + "bpm");

      alertDialogBuilder.setPositiveButton("yes", new DialogInterface.OnClickListener() {
         @Override
         public void onClick(DialogInterface arg0, int arg1) {
            // Do what needs to be done
         }
      });

      alertDialogBuilder.setNegativeButton("No",new DialogInterface.OnClickListener() {
         @Override
         public void onClick(DialogInterface dialog, int which) {
            finish();
         }
      });

      AlertDialog alertDialog = alertDialogBuilder.create();
      alertDialog.show();

使用上面的代码在警告对话框中显示您的消息。根据您的需要定制。有关详细信息,请参阅this