简体   繁体   English

如何在一个活动中将图像从ImageView发送到另一个活动中的ImageButton。 Android Studio

[英]How can I send an image from an ImageView in one activity to an ImageButton in another activity. Android Studio

I am trying to modify an image of an ImageView in one activity using a spinner, and then send that image to an ImageButton in another activity. 我正在尝试使用微调器在一个活动中修改ImageView的图像,然后将该图像发送到另一个活动中的ImageButton。

Here is the code of the ImageView activity: 这是ImageView活动的代码:

    imgView = (ImageView) findViewById(R.id.imgView);
    spinner = (Spinner) findViewById(R.id.spinner);
    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            Intent imgIntent = getIntent();

            switch (position) {
                case 0:
                    imgView.setImageResource(R.drawable.brown);
                    imgIntent.putExtra("image", R.drawable.brown);
                    break;

                case 1:
                    imgView.setImageResource(R.drawable.blue);
                    imgIntent.putExtra("image", R.drawable.blue);
                    break;

                case 2:
                    imgView.setImageResource(R.drawable.black);
                    imgIntent.putExtra("image", R.drawable.black);
                    break;

                case 3:
                    imgView.setImageResource(R.drawable.white);
                    imgIntent.putExtra("image", R.drawable.white);
                    break;

                default:
                    imgView.setImageResource(R.drawable.brown);
                    imgIntent.putExtra("image", R.drawable.brown);
                    break;
            }

        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });

Here is the code of the ImageButton activity: 这是ImageButton活动的代码:

    ImageButton imgBtn = (ImageButton) findViewById(R.id.imgBtn);
    Intent imgIntent = getIntent();
    imgBtn.setImageResource(imgIntent.getIntExtra("image"));

I cannot figure out how to do this. 我不知道该怎么做。

Full code of MainActivity: MainActivity的完整代码:

public class MainActivity extends AppCompatActivity { 公共类MainActivity扩展了AppCompatActivity {

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

    ImageButton imgBtn = (ImageButton) findViewById(R.id.imgBtn);
    Intent imgIntent = getIntent();
    imgBtn.setImageResource(imgIntent.getIntExtra("image", 0));
    startActivity(imgIntent);

    imgBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(getApplicationContext(), CustomizeDoggo.class);
            startActivity(intent);


        }
    });
}

} }

just change this line.You haven't passed the default value 只需更改此行即可。您尚未传递默认值

imgBtn.setImageResource(imgIntent.getIntExtra("image",0));

and u haven't started the activity 而且你还没有开始活动

startActivity(mIntent);

UPDATE UPDATE
Start an intent like this 开始这样的意图

//This line is missing in your code 
Intent intent = new Intent(CurrentActivity.this,ActivityToOpen.class);
intent.putExtra("image",id of selected image);
startActivity(intent);

在dest Activity中:在onCreate()方法中调用getArgurment()以获取包,然后在getIntExtra("image",default value);

wait try this will surely work 等待尝试这肯定会工作

In main activity while starting new activity use this code instead of yours 在开始新活动的主要活动中,使用此代码代替您的代码

Intent=new Intent(mContext,SecondActivity.class);
intent.putExtra("image",R.drawable.blue);
startActivity(intent);

now in second activity in oncreate use this code 现在在oncreate的第二个活动中使用此代码

int image=getIntent().getExtras().getInt("image",0);
mPlayPauseImageButton.setImageResource(image);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android - 如何将ImageView从一个活动转移到另一个活动? - Android - how can i transfer ImageView from one activity to another activity? 如何在Kitkat Android中将图像从一个活动发送到另一个活动? - How to send image from one activity to another in kitkat android? 如何在Android中将图像从一个活动发送到另一个活动 - How to send image from one activity to another in android 如何在Android中将哈希映射数组从一个活动发送到另一个活动 - How to send array of Hashmap from one activity to another activity in android 如何将当前位置从一个活动发送到另一个活动? - How can I send current location from one activity to another? 当我单击图像按钮时,如何将 ImageButton 放入会导致另一个活动的片段中 - How can I put ImageButton in Fragment that will lead to another activity when I clicked the Image Button Android-如何更新其他活动的数据。 在您正在从事的工作之外 - Android- How to update data on another activity. Outside the one you're working on 将图像从一项活动发送到另一项活动 - send image from one activity to another 我需要不断地将数据从一个活动发送到Android中的另一个活动 - I need to constantly send data from one activity to another in Android 如何在android中将Calendar对象从一个活动发送到另一个活动? - How to send a Calendar object from one activity to another in android?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM