【问题标题】:How to call a button from another button in another class?如何从另一个类中的另一个按钮调用一个按钮?
【发布时间】:2020-02-20 19:58:55
【问题描述】:

我的 MainActivity 中有一个按钮,我想在另一个按钮内的 SecondActivity 中调用它,基本上两个按钮都做同样的事情

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

【问题讨论】:

  • 分离视图(活动/片段)和业务逻辑。
  • 我需要更多详细信息
  • 您可以将 MainActivity 共享按钮设为静态公共属性,并像 MainActivity.share.performClick() 这样在 secondActivity 中调用它,但不推荐这种方法,我相信它不会像这两个活动都将在后台
  • 了解不同的设计模式和架构,如 MVC、MVP、MVVM 等。它们都是关于将您的视图与业务逻辑分离。
  • @mhemdan 将按钮设为静态将无济于事,除非您在 Java 代码中将按钮添加到布局中。这也是一种非常糟糕的做法。

标签: java android function button methods


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

【讨论】:

  • 对不起@IshakBenaissa,但如果你不给我更多关于你的崩溃的信息,我无法帮助你......
  • 我什至不知道怎么做,我用过的方法效果很好,但是当我尝试复制你的方法时它崩溃了
猜你喜欢
  • 1970-01-01
  • 2020-08-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-23
  • 1970-01-01
相关资源
最近更新 更多