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

Java 如何从另一个类中的另一个按钮调用按钮?,java,android,function,button,methods,Java,Android,Function,Button,Methods,我的主活动中有一个按钮我想在第二个活动中调用它在另一个按钮中基本上两个按钮都做相同的事情 main活动 share.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Drawable myDrawable = scannedImageView.getDrawable(); Bitmap bi

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

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
        }
    });
第二活动

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

在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的观点,最好将视图(活动/片段)和业务逻辑分开

将视图(活动/片段)分开还有您的业务逻辑。我需要更多详细信息您可以将MainActivity share button设置为静态公共属性,并在secondActivity中调用它,如MainActivity.share.performClick(),但不建议使用这种方法,我相信它不会在总体上起作用,因为这两种活动都是关于不同设计模式和架构(如MVC、MVP、MVVM等)的后台阅读。它们都是关于将您的视图与业务逻辑分离。@mhemdan将按钮设为静态不会有帮助,除非在java代码中将按钮添加到布局中。这也是一个非常糟糕的做法。抱歉@IshakBenaissa,但如果你不给我更多关于你的崩溃的信息,我也帮不了你…我甚至不知道怎么做,我用过的方法很好,但当我尝试复制你的方法时,它崩溃了