【问题标题】:ImageView onClick runs only one timeImageView onClick 只运行一次
【发布时间】:2023-01-10 18:57:50
【问题描述】:

我正在开发简单的 webview 应用程序,并想添加一些图像作为按钮,当您单击它时,它会打开共享意图。

网页视图.java OnCreate 中的这段代码

ImageView ShareButton = (ImageView) findViewById(R.id.ShareButton);
        ShareButton.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                try {
                    Intent sendIntent = new Intent();
                    sendIntent.setAction(Intent.ACTION_SEND);
                    sendIntent.putExtra(Intent.EXTRA_TEXT, "URL" );
                    sendIntent.setType("text/plain");
                    Intent shareIntent = Intent.createChooser(sendIntent, null);
                    startActivity(shareIntent);
                    Toast.makeText(getApplicationContext(), "Button clicked!", Toast.LENGTH_SHORT).show();

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

在第一次安装和第一次运行 webview 活动时,onclick 方法有效,但如果我关闭应用程序并再次重新打开它,onclick 不再工作,我必须清除数据/缓存或重新安装。

这里发生了什么事?

我尝试在 OnCreate 外部创建单独的方法,然后在 OnCreate 内部调用它,但它也不起作用。

【问题讨论】:

  • 它会进入异常块吗?如果它进入异常块,请添加日志。

标签: android


【解决方案1】:

我在 onCreate 中尝试这段代码,它工作正常

 ImageView ShareButton = (ImageView) findViewById(R.id.imageView);
        ShareButton.setOnClickListener(v -> {
            try {
                Intent sendIntent = new Intent();
                sendIntent.setAction(Intent.ACTION_SEND);
                sendIntent.putExtra(Intent.EXTRA_TEXT, "URL" );
                sendIntent.setType("text/plain");
                Intent shareIntent = Intent.createChooser(sendIntent, null);
                startActivity(shareIntent);
                Toast.makeText(getApplicationContext(), "Button clicked!", Toast.LENGTH_SHORT).show();

            } catch (Exception e) {
                e.printStackTrace();
            }
        });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-13
    • 2017-10-17
    • 2013-11-13
    • 2017-01-24
    • 1970-01-01
    相关资源
    最近更新 更多