有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何从另一个类中的另一个按钮调用按钮?

我的主活动中有一个按钮我想在第二个活动中调用它在另一个按钮中基本上两个按钮都做相同的事情

main活动

 share.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Drawable myDrawable = scannedImageView.getDrawable();
            Bitmap bitmap = ((BitmapDrawable)myDrawable).getBitmap();
            try{
                File file = new File(MainActivity.this.getExternalCacheDir(), "myImage.png");
                FileOutputStream fOut = new FileOutputStream(file);
                bitmap.compress(Bitmap.CompressFormat.PNG, 80, fOut);
                fOut.flush();
                fOut.close();
                file.setReadable(true, false);
                Intent intent = new Intent(Intent.ACTION_SEND);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
                intent.setType("image/png");
                startActivity(Intent.createChooser(intent, "Share Image Via"));
            }catch (FileNotFoundException e){
                e.printStackTrace();
                Toast.makeText(MainActivity.this, "File not found", Toast.LENGTH_SHORT).show();
            }catch (IOException e){
                e.printStackTrace();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    });

第二活动

 share.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           //Calling button from MainActivity
        }
    });

共 (1) 个答案

  1. # 1 楼答案

    在utils类中为返回OnClickListener的示例创建一个静态方法

    public static View.OnClickListener getShareButtonClickListener(Activity activity) {
        return new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Drawable myDrawable = scannedImageView.getDrawable();
                Bitmap bitmap = ((BitmapDrawable)myDrawable).getBitmap();
                try{
                    File file = new File(activity, "myImage.png");
                    FileOutputStream fOut = new FileOutputStream(file);
                    bitmap.compress(Bitmap.CompressFormat.PNG, 80, fOut);
                    fOut.flush();
                    fOut.close();
                    file.setReadable(true, false);
                    Intent intent = new Intent(Intent.ACTION_SEND);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
                    intent.setType("image/png");
                    activity.startActivity(Intent.createChooser(intent, "Share Image Via"));
                }catch (FileNotFoundException e){
                    e.printStackTrace();
                    Toast.makeText(activity, "File not found", Toast.LENGTH_SHORT).show();
                }catch (IOException e){
                    e.printStackTrace();
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        };
    }
    

    在这两项活动中

    share.setOnCLickListener(MyUtilsClass.getShareButtonClickListener(this))
    

    但我同意@Abbas的观点,最好将您的观点(活动/片段)和业务逻辑分开