想要制作一个以主布局开头的 Android 应用程序,当您按下此布局中的按钮(称为stateButton)时,布局更改为包含另一个按钮(称为boton2 )的main2布局,当您按下此按钮时,您会得到回到第一主线。
我想在同一个活动中执行此操作,而不创建或启动另一个活动。
在这里,我向您展示部分代码:
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);
}
}
}
主电源只有一些图像、文本视图和按钮。
但我有一些麻烦。不能就这么简单吗?或者我错过了什么或出了什么问题?