0

我想知道当你想显示另一个屏幕时,为什么人们一直建议开始新的活动?

假设我想显示一个带有标签和 edit_text 的屏幕来询问用户名,然后是另一个类似的屏幕来询问年龄,然后是另一个屏幕来显示输入的数据并要求确认。

我这样做了:

main_layout.xml:   has a button let's say mainButton, onClick="startRegistration"
name_layout.xml:      edittext asking for name
age_layout.xml:        edittext asking for age
confirm_layout.xml:    display info + button to confirm

并在:

public class MainActivity extends Activity {

onCreate(...) {
   ...
   setContentView(R.layout.main_layout);
}

public void startRegistration(View clickedButton) {
   setContentView(R.layout.name_layout);
}
..
}

...依此类推,所有按钮处理程序都是主类中的公共 void 方法,每个方法都包含setContentView()下一个布局作为参数。

我觉得这是一种糟糕的编程风格,但它工作得非常好。这样做可以吗?如果没有,还有其他简单的方法吗?为这些事情开始一项新活动对我来说真的很愚蠢。

4

2 回答 2

0

If you code different layouts for the same type of screen then its not really an ideal idea. The better idea is to have the same layout and point to the same layout from the classes where the layouts are the same. In the screen where you want to have an extra/less control or different control then just have unique IDs to such controls.

Refer the controls from their IDs and you will have a single layout file. Writing different layout classes where the controls are same will pave way to code repetition and hence is not an ideal way of coding.

于 2012-12-03T16:12:55.713 回答
0

通常,您将“活动”组合在一个活动中。对您而言,注册使用多个屏幕,但彼此链接。我建议将 1 Activity 与 ViewFlipper 一起使用。

为所有人拥有 1 个活动将搞砸用户的导航。Back 键必须特别处理。“如果返回键,设置此内容,否则设置此内容等”

于 2012-12-03T15:06:06.320 回答