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


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

最佳答案

只需设置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();
                }
            }
     });

10-08 17:58