根据活动按钮可见性

时间:2018-05-05 11:17:18

标签: android

我有三个活动AB和C.我在活动C中有一个按钮。我的要求是按钮只有在从活动B到C时才可见。从A到C时按钮应该是隐形的。请帮助我

1 个答案:

答案 0 :(得分:0)

活动A

public class A extends AppCompatActivity {
    Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.yourLayout);
        // TODO: 5/5/2018 consider findViewById 
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(A.this, C.class);
                intent.putExtra(KEY_EXTRA, FROM_A);
                startActivity(intent);
            }
        });
    }
}

活动B

public class B extends AppCompatActivity {
    Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.yourLayout);
        // TODO: 5/5/2018 consider findViewById
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(B.this, C.class);
                intent.putExtra(KEY_EXTRA, FROM_B);
                startActivity(intent);
            }
        });
    }
}

活动C

public class C extends AppCompatActivity {
    Button button;

    public static int FROM_A = 1;
    public static int FROM_B = 2;
    public static String KEY_EXTRA = "KEY_EXTRA";

    int activityStartedFrom;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.yourLayout);
        // TODO: 5/5/2018 consider findViewById
        activityStartedFrom = getIntent().getIntExtra(KEY_EXTRA, FROM_B);
        button.setVisibility(activityStartedFrom == FROM_B ? View.VISIBLE : View.GONE);
    }
}