如何使用不同的按钮访问同一个类?

时间:2015-11-12 15:59:13

标签: android image android-intent android-camera

我有三项活动。

A>> B>> C

C&gt;&gt;将图片和值返回B&gt;&gt;返回A&gt;&gt;如果textView不为null,则可以B < / p>

C具有相机功能,可将imageB返回到A

A中,它有一个editText和一个button。如果editText不是null,则可以通过传递Bvalue进行编辑来点击image。但现在的问题是,当我点击按钮时,我会看到image view上的图像显示,即使它没有从c获取图像!

我知道我可以使用view.setImageDrawable(null)清除图片,但由于我使用不同的小部件访问B两次,因此无法在我的情况下使用!

活动C

  ok.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View arg0)  //return to B
            {
                Intent returnIntent=new Intent();
                text=t.getText().toString();
                b.setDrawingCacheEnabled(true);
                b.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
                        View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
                b.layout(0, 0, b.getMeasuredWidth(), b.getMeasuredHeight());
                b.buildDrawingCache(true);
                returnIntent.putExtra("text", text);
                if (b.getDrawingCache() != null) {
                    Bitmap bitmap = Bitmap.createBitmap(b.getDrawingCache());
                    if (bitmap == null) {
                        Log.e("TAG", "getDrawingCache() == null");
                    }
                    Global.img = bitmap;
                }
                setResult(Activity.RESULT_OK, returnIntent);
                finish();
            }
        });
    }

活动B

 public void onActivityResult(int requestCode,int resultCode, Intent data)
    {  //receive value and image from C
        if(requestCode==PROJECT_REQUEST_CODE) {
            if(data!=null&&data.hasExtra("text")) {
                c = data.getStringExtra("text");
                txt1.setText(c);
                viewImage.setImageBitmap(Global.img);
            }


        }
        else if (requestCode==CAMERA_REQUEST_CODE)
        {

        }
    }

  b.setOnClickListener(new View.OnClickListener() {  // back to A
            public void onClick(View arg0) {
                Intent returnIntent = new Intent();
                a = "Project";
                text = txt.getText().toString(); // amount
                returnIntent.putExtra("text", text);
                returnIntent.putExtra("a", a);
                final int k1 = getIntent().getExtras().getInt("k");
                returnIntent.putExtra("k1", k1);
                returnIntent.putExtra("c",c);
                setResult(Activity.RESULT_OK, returnIntent);
                finish();
            }
        });

活动A

 @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {  //receive data from B
        int button = data.getIntExtra("k1", 0);

        if (button == 1) {
            switch (requestCode) {
                case 0:
                    result = data.getStringExtra("text");
                    name = data.getStringExtra("a");
                    description=data.getStringExtra("c");
                    if (Global.img != null) {
                        v.setImageBitmap(Global.img);
                    }

                     as=Long.parseLong(result);
                    c.setText("            " + name + "------" + "RM " + result);
                    break;
}

 c.setOnClickListener(new View.OnClickListener() { // if c is not null, it can intent to B.Otherwise it is not clickable
  @Override
      public void onClick(View view) {
      if ((name != null && name.trim().length() > 0) && (result != null && result.trim().length() > 0)) {
      Toast.makeText(getActivity().getApplicationContext(), "not null", Toast.LENGTH_LONG).show();
       if (name.equals("Project")) {
       Intent intent = new Intent(getActivity(), Project1.class);
       Global.img = null;
       v.setDrawingCacheEnabled(true);
       v.measure(View.MeasureSpec.makeMeasureSpec(0,               View.MeasureSpec.UNSPECIFIED),
       View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
       v.getMeasuredWidth(), v.getMeasuredHeight());
       v.buildDrawingCache(true);
       intent.putExtra("name", name);
       intent.putExtra("result", result);
       intent.putExtra("description", description);
       if (v.getDrawingCache() != null) {
       Bitmap bitmap = Bitmap.createBitmap(v.getDrawingCache());
       if (bitmap == null) {
       Log.e("TAG", "getDrawingCache() == null");
                             }
       Global.img = bitmap;
       startActivity(intent);
                                             }
                                         } 

  button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Toast.makeText(getActivity().getApplicationContext(), fk + "", Toast.LENGTH_LONG).show();
                AlertDialogRadio(a1);


            }
        });

 public void AlertDialogRadio(final int k) {
         final CharSequence[] ClaimsModel = {"B", "Petrol", "Car Maintenance"};

        AlertDialog.Builder alt_bld = new AlertDialog.Builder(getActivity());
        alt_bld.setTitle("Select a Claims");
        alt_bld.setSingleChoiceItems(ClaimsModel, -1, new DialogInterface
                .OnClickListener() {

            public void onClick(DialogInterface dialog, int item) {
                if (item == 0) {
                    Intent intent = new Intent(getActivity().getApplicationContext(), B.class);
                   intent.putExtra("k",k);
                    startActivityForResult(intent, 0);
                }else{...}
}

在A中我有textView和按钮。如果单击textView,它应该在B imageView上显示图像。如果单击按钮,则B imageView上不应显示任何内容。请帮忙,我不知道......

1 个答案:

答案 0 :(得分:0)

只需在intent.putExtra("bitmap",true);中添加c.setOnClickListener,然后添加

即可
 viewImage = (ImageView) findViewById(R.id.imageView2);
        if(getIntent().getBooleanExtra("bitmap",false))
        {
            viewImage.setImageBitmap(Global.img);
        }
        else
        {
            viewImage.setImageBitmap(null);
        }
在onCreate Activity B中