【问题标题】:How do I sent data from checkbox to Main2activity如何将数据从复选框发送到主活动
【发布时间】:2020-07-05 12:14:55
【问题描述】:

我想将颜色发送到另一个活动

我触摸 ImageView 以获取颜色并在复选框上显示 setBackgroundColor

   public boolean onTouch(View view, MotionEvent motionEvent) {

            if (motionEvent.getAction() == MotionEvent.ACTION_DOWN || motionEvent.getAction() == MotionEvent.ACTION_MOVE) {
                counter++;
                bitmap = mImageView.getDrawingCache();
                int pixel = bitmap.getPixel((int) motionEvent.getX(), (int) motionEvent.getY());

                int r = Color.red(pixel);
                int g = Color.green(pixel);
                int b = Color.blue(pixel);

                if (counter == 1) {
                    checkBox1.setBackgroundColor(Color.rgb(r, g, b));
                    checkBox1.setText("R:" + r + " G:" + g + " B:" + b);

                } else if (counter == 2) {
                    checkBox2.setBackgroundColor(Color.rgb(r, g, b));
                    checkBox2.setText("R:" + r + " G:" + g + " B:" + b);
                } else if (counter == 3) {
                    checkBox3.setBackgroundColor(Color.rgb(r, g, b));
                    checkBox3.setText("R:" + r + " G:" + g + " B:" + b);
                }
            }

            return true;
        }

然后我想从复选框中选择一种或多种颜色并发送到 Main2activity 并显示在 TextView 上

      mSave.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(Main3Activity.this,Main4Activity.class);      
            startActivity(intent);
        }
    });
}

【问题讨论】:

    标签: android checkbox


    【解决方案1】:

    将数据从当前活动发送到目标活动

    Intent intent = new Intent(this, DestinationActivityName.class);
    intent.putExtra(Key, Value);
    startActivity(intent);
    

    检索数据

    Intent intent = getIntent();
    String str = intent.getStringExtra(Key);
    

    为您的第二个活动创建一个 Id 并将背景设置为 Color.parseColor(str);

    更多信息:https://www.dev2qa.com/passing-data-between-activities-android-tutorial/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-28
      • 2017-03-03
      • 1970-01-01
      相关资源
      最近更新 更多