从一种布局视图更改为另一种布局视图并返回?
想要制作一个以 main 布局开始的 Android 应用程序,当您按下此布局中的按钮(称为 stateButton)时,布局将更改为 main2 布局包含另一个按钮(称为 boton2),当您按下此按钮时,您将返回到第一个主按钮。
我想在同一个活动中执行此操作,而不创建或启动另一个活动。
这里我向您展示部分代码:
public class NuevoshActivity extends Activity
implements SensorEventListener, OnClickListener {
private Button stateButton;
private Button boton2;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.stateButton = (Button) this.findViewById(R.id.boton);
this.boton2 = (Button) this.findViewById(R.id.boton2);
stateButton.setOnClickListener(this);
boton2.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if(v==stateButton) {
setContentView(R.layout.main2);
}
else if(v==boton2) {
setContentView(R.layout.main);
}
}
}
主电源只有一些图像、文本视图和按钮。
但我有一些麻烦。难道就不能这么简单吗?或者我错过了什么或者出了什么问题?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当您使用 findViewById 时,您实际上是在尝试在 setContentView 指定的布局内查找视图。因此,当您尝试检查按钮时,一次又一次地使用 setContentView 可能会带来问题。
我不会使用 setContentView,而是将屏幕的 2 个布局添加为视图翻转器的子布局,该视图翻转器一次只显示一个子视图。并且您可以指定要显示哪个子项的索引。使用视图翻转器的好处是,如果在视图之间切换时需要动画,您可以轻松地为视图指定“进入”和“退出”动画。这是一个比一次又一次调用 setContentView 更干净的方法。
When you use findViewById, you are actually trying to find a view inside the layout you specified by the setContentView. So using setContentView again and again might bring problems when you are trying to check for buttons.
Instead of using a setContentView, I would add the 2 layouts for the screen as child's for a view-flipper which only shows one child at a time. And you can specify the index of which child to show. The benefit of using a view flipper is that you can easily specify a 'in' and 'out' animation for the view if you need an animation when you switch between views. This is a lot cleaner method then recalling setContentView again and again.
FrameLayout
很好地处理了这个问题...将其与setvisibility(View.VISIBLE);
和setVisibility(View.INVISIBLE);
。例如:
主要 XML 包括两个其他布局:
在布局之间切换的代码:
您还需要在活动启动时(或在 XML 中)设置各个布局的可见性,否则您将看到它们两个 - 一个在另一个之上。
The
FrameLayout
handles this wonderfully... Use this with the<include...
contstruct to load multiple other layouts, then you can switch back and forth between them by usingsetvisibility(View.VISIBLE);
andsetVisibility(View.INVISIBLE);
on the individual layouts.For example:
Main XML including two other layouts:
Code to switch between layouts:
You will also need to set the visibility of the individual layouts when the activity starts (or in XML) otherwise you will see them both - one on top of the other.
您的
boton2
按钮将为 NULL,因为该按钮的定义位于main2.xml
中。您能够找到的唯一视图是在
main.xml
中定义的视图。Your
boton2
button will be NULL because the definition of the button is inmain2.xml
.The only views you will be able to find are the views which are defined in
main.xml
.谢谢!!!所有信息对于理解很多事情都很有用,正如 C0deAttack 评论的那样,我在使用 main2 上的按钮时遇到了麻烦。我所做的是将 View.VISIBLE 和 View.GONE 设置为我想要在每个布局中使用的 TextViews 和 Button。非常感谢。
Thanks!!! All the info was usefull to understand a lot of things and as C0deAttack commented I've got troubles with the button on the main2. What I've done is to set View.VISIBLE and View.GONE to the TextViews and Buttons that I wanted in each layout. Thank you very much.