【发布时间】:2026-02-03 20:35:01
【问题描述】:
想要制作一个以 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);
}
}
}
主要只有一些图像、文本视图和按钮。
但我有一些麻烦。难道就不能这么简单吗?或者我错过了什么或出了什么问题?
【问题讨论】:
标签: android android-layout android-button