如何将多个选中复选框中的文本转换为电子邮件

时间:2016-06-02 23:20:14

标签: java

我是非常新的,并希望帮助弄清楚如何将CheckBoxes中的所有文本添加到电子邮件正文段落作为列表,并且有很多麻烦。当我按下发送按钮时,它会导致应用程序崩溃。

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;

public class breakfast extends AppCompatActivity {
CheckBox b1;
CheckBox b2;
CheckBox b3;
CheckBox b4;
CheckBox b5;
CheckBox b6;
CheckBox b7;
CheckBox b8;
CheckBox b9;
CheckBox b10;
CheckBox b11;
Button sendbutton;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_breakfast);
    sendbutton = (Button) findViewById(R.id.bb);
    b1 = (CheckBox) findViewById(R.id.b1);
    b2 = (CheckBox) findViewById(R.id.b2);
    b3 = (CheckBox) findViewById(R.id.b3);
    b4 = (CheckBox) findViewById(R.id.b4);
    b5 = (CheckBox) findViewById(R.id.b5);
    b6 = (CheckBox) findViewById(R.id.b6);
    b7 = (CheckBox) findViewById(R.id.b7);
    b8 = (CheckBox) findViewById(R.id.b8);
    b9 = (CheckBox) findViewById(R.id.b9);
    b10 = (CheckBox) findViewById(R.id.b10);
    b11 = (CheckBox) findViewById(R.id.b11);
//b# is all of the checkboxes 
//this is where i'm not too sure.
    final Intent email = new Intent(Intent.ACTION_SEND);
    email.setType("message/rfc822");
//email intent where key word is email type is set to email only

我认为我的代码存在问题。

sendbutton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {


            boolean checked = ((CheckBox) v).isChecked();

            switch (v.getId()) {
                case R.id.b2:
                    if (checked)
                        email.putExtra(Intent.EXTRA_TEXT, b2.getText().toString());
                case R.id.b3:
                    if (checked)
                        email.putExtra(Intent.EXTRA_TEXT, b3.getText().toString());
                case R.id.b4:
                    if (checked)
                        email.putExtra(Intent.EXTRA_TEXT, b4.getText().toString());
                case R.id.b5:
                    if (checked)
                        email.putExtra(Intent.EXTRA_TEXT, b5.getText().toString());
                case R.id.b6:
                    if (checked)
                        email.putExtra(Intent.EXTRA_TEXT, b6.getText().toString());
                case R.id.b7:
                    if (checked)
                        email.putExtra(Intent.EXTRA_TEXT, b7.getText().toString());
                case R.id.b8:
                    if (checked)
                        email.putExtra(Intent.EXTRA_TEXT, b8.getText().toString());
                case R.id.b9:
                    if (checked)
                        email.putExtra(Intent.EXTRA_TEXT, b9.getText().toString());
                case R.id.b10:
                    if (checked)
                        email.putExtra(Intent.EXTRA_TEXT, b10.getText().toString());
                case R.id.b11:
                    if (checked)
                        email.putExtra(Intent.EXTRA_TEXT, b11.getText().toString());
                    break;
            }
            startActivity(Intent.createChooser(email, "Send Email"));


        }
    });
}

}

2 个答案:

答案 0 :(得分:0)

此声明可能导致此问题: captureSession.startRunning() let visibleRect = previewLayer.metadataOutputRectOfInterestForRect(previewLayer.bounds) captureMetadataOutput.rectOfInterest = visibleRect 在这种情况下, boolean checked = ((CheckBox) v).isChecked();代表vsendbutton,无法投放到Button,因此崩溃。

答案 1 :(得分:0)

如果您创建所有复选框的ArrayList会更好:

public class breakfast extends AppCompatActivity {
   CheckBox b1;
   CheckBox b2;
   CheckBox b3;
   CheckBox b4;
   CheckBox b5;
   CheckBox b6;
   CheckBox b7;
   CheckBox b8;
   CheckBox b9;
   CheckBox b10;
   CheckBox b11;
   Button sendbutton;

   // List of all the checkbozes...
   List<CheckBox> checkBoxes = new ArrayList<CheckBox>();
   checkBoxes.add(b1);
   checkBoxes.add(b2);

   // add all the rest up to b11...

现在,您可以循环遍历ArrayList中的所有复选框,而不是在onClick处理程序中使用switch语句。

sendbutton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        for (CheckBox cb : checkBoxes)
        {
            // if the cb is checked
            {
               email.putExtra(Intent.EXTRA_TEXT, cb.getText().toString());
            }
        }

        startActivity(Intent.createChooser(email, "Send Email"));
    }
 }