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

标签 java android function button methods

我的 MainActivity 中有一个按钮,我想在另一个按钮内的 SecondActivity 中调用它,基本上两个按钮都执行相同的操作

主要 Activity

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

第二个 Activity

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

在这两项 Activity 中

share.setOnCLickListener(MyUtilsClass.getShareButtonClickListener(this))

但我同意@Abbas 的观点,最好将您的 View ( Activity/fragment )和业务逻辑分开

关于java - 如何从另一个类中的另一个按钮调用一个按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58542025/

相关文章:

c - C函数中的参数传递

带有内置函数的 Python inspect.getargspec

android - 具有固定第一列和其余列可滚动 Android 的 TableView

java - gc.log写入是异步的吗?将 gc.log 放在 NFS 挂载上安全吗?

java - 解析和读取一个json数组java

Java - 树中的缩进列表

java - Android/Java 简单的先进先出缓冲区

java - 从 JSON 到没有表名的对象

r - 通过 R 中的嵌套函数传递默认参数

java - 在每个 @Test 范围之间请求新的单例 bean "WebDriver"