简体   繁体   English

使用android中的putextra和getextra方法从第二个活动转到第一个活动时图像视图的可见性消失了

[英]visibility gone of image view when come from 2nd activity to 1st activity using putextra and getextra method in android

everyone. 大家。 I am new in android. 我是android新手。 I am creating two activities in android ,1st activity has two image view and one button . 我在android中创建两个活动,第一个活动有两个图像视图和一个按钮。 and 2nd activity has one button. 第二活动有一个按钮。 when I go to second activity and then from second activity agian . 当我参加第二次活动,然后从第二次活动中离开。 one image view of 1st activity should visibility gone. 第一项活动的一个图像视图应该没有可见性。 how can i do this. 我怎样才能做到这一点。

here is my code 这是我的代码

Activitymain.xml


  <ImageView
        android:id="@+id/1stimage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <ImageView
        android:id="@+id/2ndimage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="go to second activity" />


   mainactivity.java



     ImageView imageView1, imageView2;
            Button btn;

            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
                imageView1 = (ImageView) findViewById(R.id.1stimage);
                imageView2 = (ImageView) findViewById(R.id.2ndimage);
                btn = (Button) findViewById(R.id.btn1);
                btn.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View arg0) {

                        Intent Firstintent= new Intent(MainActivity.this,SecondActivity.class);
                        startActivity(Firstintent);

                    }
                });

            }
secondactivity.xml

    <Button
            android:id="@+id/btn2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="go to 1st activity" />

secondactivity.java

    Button btn;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_second);
            btn = (Button) findViewById(R.id.btn2);
            btn.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {

                    Intent SecondIntent = new Intent(SecondActivity.this,
                            MainActivity.class);
                    SecondActivity.this.startActivity(SecondIntent);
                    SecondActivity.this.finishActivity();

                }
            });
        }

please help me 请帮我

Use putextra(tag, some variable to use in mainActivity) and Bundle extras = getIntent().getExtras(); extras.get[type of variable](tag) 使用putextra(tag, some variable to use in mainActivity)Bundle extras = getIntent().getExtras(); extras.get[type of variable](tag) Bundle extras = getIntent().getExtras(); extras.get[type of variable](tag) to pass a variable (probably a boolean) to tell the onCreate() to show or not show the pic. Bundle extras = getIntent().getExtras(); extras.get[type of variable](tag)传递一个变量(可能是布尔值)来告诉onCreate()显示或不显示图片。

EDIT: Here is one way to implement it: in the first activity: 编辑:这是一种实现它的方法:在第一个活动中:

1) Check if there are extras (AKA if second activity was loaded/ there is a true) 1)检查是否有其他功能(如果已加载第二个活动/则为真)

2) If (yes) { if (first pic is loaded) { remove first pic } else if (first pick is not but second is loaded) { remove second pic } ... for all the pictures you want to do this to. 2)如果(是){如果(加载了第一张照片){删除了第一张照片}否则,如果(没有第一张照片但加载了第二张照片){删除了第二张照片} ...对于要执行此操作的所有图片。

Remember you only need to pass true/false from the second activity (and then handle it in the first/one being modified) 记住,您只需要在第二个活动中传递true / false(然后在被修改的第一个/一个中处理它)

Activitymain.xml


  <ImageView
        android:id="@+id/1stimage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <ImageView
        android:id="@+id/2ndimage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="go to second activity" />


   mainactivity.java



     ImageView imageView1, imageView2;
            Button btn;

            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
                imageView1 = (ImageView) findViewById(R.id.1stimage);
                imageView2 = (ImageView) findViewById(R.id.2ndimage);
                btn = (Button) findViewById(R.id.btn1);
                btn.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View arg0) {

                        Intent Firstintent= new Intent(MainActivity.this,SecondActivity.class);
                        startActivity(Firstintent);

                    }
                });
                Bundle extras = getIntent().getExtras();
                if(extras != null){
                   if(extras.getBoolean("isResult", false)){
                      imageView1.setVisability(View.GONE);
                   }
                }

            }
secondactivity.xml

    <Button
            android:id="@+id/btn2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="go to 1st activity" />

secondactivity.java

    Button btn;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_second);
            btn = (Button) findViewById(R.id.btn2);
            btn.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {

                    Intent SecondIntent = new Intent(SecondActivity.this,
                            MainActivity.class);
                    SecondIntent.putExtra("isResult", true);
                    SecondActivity.this.startActivity(SecondIntent);
                    SecondActivity.this.finishActivity();

                }
            });
        }

暂无
暂无

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

相关问题 从Android中的第一个应用程序开始第二个应用程序的活动 - Start Activity of 2nd app from 1st app in android 如何从第二活动到第一活动获取数据 - How to get data from 2nd Activity to 1st Activity 尝试从第一类活动中调用方法,并在第二类活动中保留相同的参数(android studio) - Trying to call a method from 1st class activity and keep the same argument in a 2nd class activity (android studio) 当我返回到第一个活动时,第二个活动未调用第二个活动的onCreate()函数 - When I return back to 1st Activity the onCreate() function of 2nd Activity is not called in 2nd time Android - 在第一个活动中使用第二个活动的字符串列表 - Android - Use String from 2nd Activities for List on 1st activity 从第一个活动拨打电话后,第二个活动没有通话 - 2nd Activity doesn't call after calling from 1st Activity 从第一个活动的 Edittext 发送字符串到第二个活动的 Edittext - Send String from Edittext on 1st Activity to the Edittext on the 2nd Activity 如何从第一个活动到第二个活动检索插入数据库中的图片? - How to retrieve a picture inserted in the database from 1st to 2nd activity? 在不丢失更新数据的情况下将第二个活动的价值保存到第一个活动 - Saving value from 2nd activity to 1st without losing the updated data 无法通过getIntent()将第一个活动的textview值传递给第二个活动的textview - Unable to pass 1st activity's textview value to 2nd activity's textview through getIntent()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM