使用ImageButton android分享到Whatsapp

时间:2015-05-11 01:38:08

标签: android android-intent android-studio whatsapp

我想使用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();
    }

我希望这里有人可以帮助我。感谢

1 个答案:

答案 0 :(得分:2)

只需设置ImageButton onClickListener

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