使用 ImageButton 分享到 Whatsapp android

Share to Whatsapp using ImageButton android

我想使用 ImageButton 将文本分享到 whatsapp,但我不知道如何设置 ImageButton 来分享文本。

这是我的代码

ImageButton wasap = (ImageButton) findViewById(R.id.wasapKongsi);
    Intent whatsappIntent = new Intent(Intent.ACTION_SEND);
    whatsappIntent.setType("text/plain");
    whatsappIntent.setPackage("com.whatsapp");
    whatsappIntent.putExtra(Intent.EXTRA_TEXT, R.id.hadisView + "/n" + R.id.textView);
    try {
        startActivity(whatsappIntent);
    } catch (android.content.ActivityNotFoundException ex) {
        Toast.makeText(hadis.this, "Whatsapp have not been installed.", Toast.LENGTH_LONG).show();
    }

我希望这里的任何人都可以帮助我。谢谢

只需设置ImageButtononClickListener

     wasap.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v) {
                //your code here that you want to run
                Intent whatsappIntent = new Intent(Intent.ACTION_SEND);
                whatsappIntent.setType("text/plain");
                whatsappIntent.setPackage("com.whatsapp");
                whatsappIntent.putExtra(Intent.EXTRA_TEXT, R.id.hadisView + "/n" + R.id.textView);
                try { 
                     startActivity(whatsappIntent);
                } catch (android.content.ActivityNotFoundException ex) {
                     Toast.makeText(hadis.this, "Whatsapp have not been installed.", Toast.LENGTH_LONG).show();
                } 
            }
     });