sendEmail方法在动态创建的按钮?

时间:2017-08-14 17:53:38

标签: android email button android-intent dynamic

我动态创建了按钮,我想通过点击这些按钮发送电子邮件,但出了点问题。什么都没发生。 :(请帮助我,我是初学者。

AdapterListingOrders:

public class AdapterListingOrders extends AppCompatActivity {

private Button btnAccRejOrders;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.adapter_listing_orders);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    btnAccRejOrders = (Button) findViewById(R.id.order_acc_rej);

    btnAccRejOrders.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.i("Send email", "");
            String[] TO = {""};
            String[] CC = {""};
            Intent emailIntent = new Intent(Intent.ACTION_SEND);

            emailIntent.setData(Uri.parse("mailto:"));
            emailIntent.setType("text/plain");
            emailIntent.putExtra(Intent.EXTRA_EMAIL, TO);
            emailIntent.putExtra(Intent.EXTRA_CC, CC);
            emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Your subject");
            emailIntent.putExtra(Intent.EXTRA_TEXT, "Email message goes here");
        }
    });
}
}

但onClick不起作用。当我按下btnAccRejOrders时,LogCat会显示:

ViewPostImeInputStage processPointer 0
ViewPostImeInputStage processPointer 1

1 个答案:

答案 0 :(得分:0)

btnAccRejOrders.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.i("Send email", "");
            Intent i = new Intent(Intent.ACTION_SEND);
            i.setType("message/rfc822");
            i.putExtra(Intent.EXTRA_EMAIL  , new String[]{"recipient@example.com"});
            i.putExtra(Intent.EXTRA_SUBJECT, "subject of email");
            i.putExtra(Intent.EXTRA_TEXT   , "body of email");
            try {
                    startActivity(Intent.createChooser(i, "Send mail..."));
                } catch (android.content.ActivityNotFoundException ex) {
                Toast.makeText(MyActivity.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
                }
        }
    });