保存复选框值并将其发送到电子邮件

时间:2017-11-28 14:29:16

标签: android

我想保存3页的复选框数据,然后用按钮将其发送到电子邮件,我在那里做错了什么?或者也许我错过了什么?我尝试了一些想法,但现在我只是坚持了它

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

    Button button = (Button)findViewById(R.id.button);
    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setClassName("com.google.android.gm", "com.google.android.gm.ComposeActivityGmail");
    intent.setType("button/press");
    intent.putExtra(Intent.EXTRA_EMAIL,new String[]{"djalise@gmail.com"});
    intent.putExtra(Intent.EXTRA_SUBJECT, "ElBoricuaSandwich");
    intent.putExtra(Intent.EXTRA_TEXT, "../chooseDrinkSize");
}

public void onCheckboxClicked(View view) {
    // Is the view now checked?
    boolean checked = ((CheckBox) view).isChecked();

    // Check which checkbox was clicked
    switch (view.getId()) {
        case R.id.checkBox5:
            if (checked) ;
                //
            else
                //
                break;
        case R.id.checkBox6:
            if (checked) ;
                //
            else
                //
                break;
            //

        case R.id.checkBox7:
            if (checked) ;
                //
            else
                //
                break;
    }
}
}

1 个答案:

答案 0 :(得分:0)

您应该创建一种方法来发送电子邮件并在发生操作时调用此方法

<强>更新

代码:

    private String emailText = "";
    private CheckBox box1;
    private CheckBox box2;
    private CheckBox box3;

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

    public void sendEmail() {
        Intent mailIntent = new Intent(Intent.ACTION_SENDTO);
        mailIntent.setData(Uri.parse("mailto:"));
        mailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{"djalise@gmail.com"});
        mailIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
        mailIntent.putExtra(Intent.EXTRA_TEXT, emailText);
        Intent mailChooserIntent = Intent.createChooser(mailIntent, "Title");
        startActivity(mailChooserIntent);
    }

    private void initViews(){
        box1 = (CheckBox) findViewById(R.id.box1);
        box2 = (CheckBox) findViewById(R.id.box2);
        box3 = (CheckBox) findViewById(R.id.box3);
        Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                buildEmailText();
                sendEmail();
            }
        });
    }

    private void buildEmailText(){
        if(box1.isChecked) emailText += "\nBox 1 is checked";
        if(box2.isChecked) emailText += "\nBox 2 is checked";
        if(box3.isChecked) emailText += "\nBox 3 is checked";
    }
    //...