如何在不使用 Intent 的情况下从其他活动中隐藏 Linier 布局?
我在这里给出了有关我的项目的详细信息,我希望当我单击按钮时,它将转到第二个活动,并且它也会从第二个活动布局中消失。
按钮 btnClick;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnClick = findViewById(R.id.btnClick);
btnClick.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, MainActivity2.class);
startActivity(intent);
MainActivity2.llOne.setVisibility(View.GONE);
}
});
} }
公共类 MainActivity2 扩展 AppCompatActivity {
public static LinearLayout llOne;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
llOne = findViewById(R.id.llOne);
}
}
I have given here details about my project and I want that when I will click on the Button it will go to the second activity and also it disappears from the second activity layout.
Button btnClick;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnClick = findViewById(R.id.btnClick);
btnClick.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, MainActivity2.class);
startActivity(intent);
MainActivity2.llOne.setVisibility(View.GONE);
}
});
}
}
public class MainActivity2 extends AppCompatActivity {
public static LinearLayout llOne;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
llOne = findViewById(R.id.llOne);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据您的评论(如果我理解正确),
MainActivity
应该使用startActivityForResult()
启动SecondActivity
。SecondActivity
会做任何事情,当返回到MainActivity
时,SecondActivity
应该返回一个带有一些“的Intent
”MainActivity
可使用它来确定要显示/隐藏的View
或Layout
内容。要返回结果,SecondActivity
应调用setResult()
。在
MainActivity
中,实现onActivityResult()
。当SecondActivity
完成时将调用此方法,并且将使用SecondActivity
传递给setResult()
Intent 来调用该方法代码>.在onActivityResult()
中,您可以检查返回的Intent
中的“额外内容”,并根据该信息您可以决定使用哪个View
或隐藏/显示的布局
。注意:如果您不需要使用
Intent
将结果传递回MainActivity
,则可以只使用resultCode
,即在int
中。Based on your comments (if I have understood correctly),
MainActivity
should launchSecondActivity
usingstartActivityForResult()
.SecondActivity
will do whatever and when it is time to return toMainActivity
,SecondActivity
should return anIntent
with some "extras" thatMainActivity
can use to determine whatView
orLayout
to show/hide. To return the result,SecondActivity
should callsetResult()
.In
MainActivity
, implementonActivityResult()
. This method will be called whenSecondActivity
finishes, and it will be called with theIntent
thatSecondActivity
passed tosetResult()
. InonActivityResult()
you can check the returnedIntent
for the "extras" and based on that information you can decide whichView
orLayout
to hide/show.Note: If you don't need to use an
Intent
to pass the result back toMainActivity
, you can just use theresultCode
, which is inint
.