【问题标题】:Use AlertDialog with canvas将 AlertDialog 与画布一起使用
【发布时间】:2017-08-30 18:45:24
【问题描述】:

我正在使用 .java 实现 surfaceView 以使用画布在屏幕上绘制,而不是使用 .xml 布局文件,但我想知道是否在某个时候(当我在视图中所做的工作完成时)可以将此视图与布局文件关联或调用按钮或alerdialog。

更清楚地说,例如当您在游戏中获胜或失败时会显示“您输了”或类似的警报对话框。

Main_Activity 看起来像:

public class Main extends Activity {

    activity_layout_animation animation;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            animation = new activity_layout_animation(this);
            setContentView(animation);
        }

        @Override
        protected void onPause(){
            super.onPause();
            animation.pause();
        }

        @Override
        protected void onResume(){
            super.onResume();
            animation.resume();
          }

视图文件的一些代码:

    public class activity_layout_animation extends SurfaceView implements Runnable {

boolean CanDraw = false
        public activity_layout_animation(Context context){
            super(context);
            surfaceHolder = getHolder();
    }
       @Override
        public void run(){
  while(CanDraw){

            if ( !surfaceHolder.getSurface().isValid()){
                continue;
            }
    }
}

编辑:我可以像这样添加一些东西:

 AlertDialog.Builder builder;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        builder = new AlertDialog.Builder(context, android.R.style.Theme_Material_Dialog_Alert);
    } else {
        builder = new AlertDialog.Builder(context);
    }
    builder.setTitle("Delete entry")
    .setMessage("Are you sure you want to delete this entry?")
    .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) { 
            // continue with delete
        }
     })
    .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) { 
            // do nothing
        }
     })
    .setIcon(android.R.drawable.ic_dialog_alert)
    .show();

但它只适用于public activity_layout_animation(Context context){,我想将它添加到run()

【问题讨论】:

  • AlertDialog 应该可以正常工作,无论是从 xml 加载内容视图,还是在代码中创建内容。您尝试调用 AlertDialog 了吗?

标签: android canvas android-alertdialog


【解决方案1】:
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context);
        LayoutInflater inflater = activity.getLayoutInflater();
        final View dialogCoordinate = inflater.inflate(R.layout.show_coordinates, null);
        dialogBuilder.setCancelable(false);
        dialogBuilder.setView(dialogCoordinate);

        ImageView imgShowCoordinates = (ImageView) dialogCoordinate.findViewById(R.id.imgShowCoordinates);
        imgShowCoordinates.setImageResource(R.drawable.whitetrans);
//        int width = dmsUtility.getScreenSize(activity)[0];
//        int height = dmsUtility.getScreenSize(activity)[1];
        Log.d("Coordinate", "showCoordinates: "+arrayList.get(pos).getComment());

        if (arrayList.get(pos).getComment() != null && !arrayList.get(pos).getComment().isEmpty()
                && !arrayList.get(pos).getComment().equals("null")) {
            String[] parts1 = arrayList.get(pos).getComment().split("[\\s\\,]+");
            Log.d(TAG, "getBody: " + arrayList.get(pos).getComment());
            float[] numbers = new float[parts1.length];

            for (int p = 0; p < numbers.length; p++) {
                numbers[p] = Math.round(Float.parseFloat(parts1[p]) * 1000) / 1000f;
            }

            bmp = ((BitmapDrawable) imgShowCoordinates.getDrawable()).getBitmap();
            if (bmp != null && !bmp.isRecycled()) {
                Bitmap tempBitmap = Bitmap.createScaledBitmap(bmp, 300, 300, true);
                tempCanvas = new Canvas(tempBitmap);
                canW = tempBitmap.getWidth();
                canH = tempBitmap.getHeight();

                for (int q = 0; q < numbers.length; q = q + 4) {
                    float x1 = (canW / 300) * numbers[0 + q];
                    if (numbers.length > q + 2) {
                        Paint paint = new Paint(Paint.LINEAR_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);
                        paint.setColor(Color.BLUE); // Text Color
                        paint.setStrokeWidth(1f); // Text Size
                        paint.setAntiAlias(true);
                        float y1 = (canH / 300) * numbers[1 + q];
                        float x2 = (canW / 300) * numbers[2 + q];
                        float y2 = (canH / 300) * numbers[3 + q];
                        tempCanvas.drawLine(x1, y1, x2, y2, paint);
                    }
                }
            }
        }

        dialogBuilder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                dialog.dismiss();
            }
        });
        AlertDialog alertDialog = dialogBuilder.create();
        alertDialog.show();

【讨论】:

  • 在没有任何解释的情况下随便乱扔代码并不是一种好的做法。您能否为您的代码添加一些解释?
猜你喜欢
  • 2013-11-14
  • 1970-01-01
  • 1970-01-01
  • 2011-09-11
  • 1970-01-01
  • 2011-08-06
  • 2021-05-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多