无法在嵌套开关盒内单击按钮
我遇到这种情况,我必须多次使用 3 个按钮,例如:
“你来自哪里”,选择是:button1、button2、button3。如果选择按钮 1,则会出现一个新问题,使用相同的按钮,但具有不同的文本。如果选择按钮 2,则会出现不同的问题,依此类推。
为此会有分层,因此按钮将更改 64 次。我正在努力解决第三层的逻辑。
我为button1的分支创建了代码:
text.setText("Where are you from?"); //this is the 1st layer
btn1.setText("Asia")
btn2.setText("Europe")
btn3.setText("North America")
case R.id.button:
branch = 1;
if (branch == 1) //this is the 2nd layer
choice = "Where in Asia are you from?"
text.setText(choice);
btn1.setText("East Asia");
btn2.setText("Southeast Asia");
btn3.setText("Northeast Asia");
break;
}
switch (view.getId()) {
case R.id.button:
choice = "Where in East Asia?" //this is the third layer
text.setText(choice);
btn1.setText("insert country");
btn2.setText("insert country 2");
btn3.setText("insert country 3");
break;
case R.id.button2:
choice = "Where in Southeast Asia"; //this is the third layer
text.setText(choice);
btn1.setText("insert country");
btn2.setText("insert country 2");
btn3.setText("insert country 3");
break;
case R.id.button3:
choice = "Where in Northeast Asia?"; //this is the third layer
text.setText(choice);
btn1.setText("insert country");
btn2.setText("insert country 2");
btn3.setText("insert country 3");
break;
东亚哪里的分支?东南亚哪里?东北亚哪里?当我运行它时没有出现。第三层看不到textview和按钮(当在第二层点击按钮时,它停留在第二层)
I have this situation where I have to use 3 buttons multiple times like:
"Where are you from" and the choices are: button1, button2, button3. if button 1 is picked then there will be a new question, using the same buttons, but with different texts. If button 2 is picked then there will be a different question and so on.
There will be layers for this so buttons will change 64 times. I am struggling with the logic for the 3rd layer.
I created codes for the branch of button1:
text.setText("Where are you from?"); //this is the 1st layer
btn1.setText("Asia")
btn2.setText("Europe")
btn3.setText("North America")
case R.id.button:
branch = 1;
if (branch == 1) //this is the 2nd layer
choice = "Where in Asia are you from?"
text.setText(choice);
btn1.setText("East Asia");
btn2.setText("Southeast Asia");
btn3.setText("Northeast Asia");
break;
}
switch (view.getId()) {
case R.id.button:
choice = "Where in East Asia?" //this is the third layer
text.setText(choice);
btn1.setText("insert country");
btn2.setText("insert country 2");
btn3.setText("insert country 3");
break;
case R.id.button2:
choice = "Where in Southeast Asia"; //this is the third layer
text.setText(choice);
btn1.setText("insert country");
btn2.setText("insert country 2");
btn3.setText("insert country 3");
break;
case R.id.button3:
choice = "Where in Northeast Asia?"; //this is the third layer
text.setText(choice);
btn1.setText("insert country");
btn2.setText("insert country 2");
btn3.setText("insert country 3");
break;
the branch for where in east Asia? where in southeast Asia? where in northeast Asia? does not appear when I run it. Both textview and buttons cannot be seen for the third layer (When button is clicked in layer 2, it stays in layer 2)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该考虑更好的数据结构来处理此逻辑。添加的层数越多,这就变得越难以处理。这是一个如何将逻辑与表示分离的简单想法:
Question-Class
所以这最终会形成一个树结构。
表示层:
文本字段和按钮仅显示与所选问题相关的信息。他们不处理任何逻辑。
您的文本字段显示问题字符串。
您的按钮将一一显示列表中的答案。
You should think about a better data structure to handle this logic. This gets unhandable the more layers you add. Here is a simple idea how separate logic from representation:
Question-Class
So this ends up in a tree structure.
Representation Layer:
The textfield and the buttons only show informations related to the selected question. They don't handle any logic.
Your textfield shows the Question-String.
Your buttons show the answers in the list one by one.