如何在Android中的其他活动中设置可见的按钮 [英] How to set a button visible from another activity in android

查看:49
本文介绍了如何在Android中的其他活动中设置可见的按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的问题.我的主活动中有一个不可见的按钮,第二个活动使该按钮可见.在第二个活动中,将按钮设置为可见没有问题,但是当我返回到Main活动时,该按钮仍然不可见.

I have a very simple problem. I have a invisible button in my Main Activity, and I have a second Activity that makes that button visible. In the second activity I don´t have a problem setting the button visible, but when I return to the Main activity the button is still invisible.

这是我的第二项活动.

Button button = (Button) inflatedView.findViewById(R.id.showButton);
if (button.getVisibility() == View.INVISIBLE){
    button.setVisibility(View.VISIBLE);
}

这是MainActivity中的resume方法

This is the resume method in the MainActivity

@Override
protected void onResume() {
    super.onResume();
}

我已经尝试使按钮在主要活动"中可见,并且可以正常工作.但是我想使按钮在第二堂课中可见.我已经尝试过将Intent从第二个活动传递到Main Activity,但是我不知道如何在Main Activity中处理Intent.我无法在onResume()或onCreate()中处理Intent,因为它将抛出 NullPointExeption .

I already tried making the button visible in the Main Activity, and worked. But I want to make the button visible from the second class. I already tried passing an Intent from the second activity to the Main Activity but I don't know how to process the Intent in the Main Activity. I can not process the Intent in the onResume() or onCreate(), because it will throw a NullPointExeption.

感谢您的帮助.

推荐答案

您应该在这两个活动之间建立通信.您可以使用 startActivityForResult()

You should set up a communication between the 2 activities. You can achieve this with startActivityForResult() and onActivityResult()

MainActivity:

MainActivity:

public class MainActivity extends Activity {

    public static final int REQUEST_CODE_SECOND_ACTIVITY = 100; // This value can be any number. It doesn't matter at all. The only important thing is to have the same value you started the child activity with when you're checking the onActivityResult.
    public static final String SHOW_BUTTON = "shouldShowButton";

    private Button mMyButtonToBeHidden;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mMyButtonToBeHidden = (Button) findViewById(R.id.buttonToBeHidden);

        findViewById(R.id.openSecondActivity).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivityForResult(new Intent(MainActivity.this, SecondActivity.class), REQUEST_CODE_SECOND_ACTIVITY);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE_SECOND_ACTIVITY && resultCode == RESULT_OK) {
            //Check if you passed 'true' from the other activity to show the button, and also, only set visibility to VISIBLE if the view is not yet VISIBLE
            if (data.hasExtra(SHOW_BUTTON) && data.getBooleanExtra(SHOW_BUTTON, false) && mMyButtonToBeHidden.getVisibility() != View.VISIBLE) {
                mMyButtonToBeHidden.setVisibility(View.VISIBLE);
            }
        }
    }
}

SecondActivity:

SecondActivity:

public class SecondActivity extends Activity {

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

        findViewById(R.id.hide_main_activity_button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.putExtra(MainActivity.SHOW_BUTTON, true);
                setResult(RESULT_OK, intent);
                finish();
            }
        });
    }
}

这篇关于如何在Android中的其他活动中设置可见的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆