gpt4 book ai didi

java - 尝试从单个类执行多个布局

转载 作者:行者123 更新时间:2023-12-01 11:09:51 25 4
gpt4 key购买 nike

我正在尝试使用 onClick() 方法从单个类执行多个布局屏幕这是我的代码

Button bt1,bt2;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt1 = (Button)findViewById(R.id.button);
bt2 = (Button)findViewById(R.id.button1);
onClick(); //onClick(View) in MainActivity cannot be applied to ()
}
public void onClick(View v){
if(v.getId()==R.id.button){
setContentView(R.layout.next1);
}
else if(v.getId()==R.id.button){
setContentView(R.layout.next1);
}
}

请大家帮帮我,谢谢

最佳答案

在这种情况下,您必须使用 fragment 。根据按钮单击加载任一 fragment 。

如果您在这种情况下使用 setContentView,那么您的代码将变得笨拙,并且跟踪 View 将非常乏味。

关于java - 尝试从单个类执行多个布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32497705/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com