Android:制作简单的代码。干

时间:2015-09-20 08:36:56

标签: java android dry

我是Java编程的新手。有人可以帮助我让我的代码保持干燥。

Button level01 = (Button) findViewById(R.id.level01);
level01.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Prefs.setStagePref(getApplicationContext(), 1);
        Intent play = new Intent(LevelActivity.this, PlayActivity.class);
        startActivity(play);
    }
});

Button level02 = (Button) findViewById(R.id.level02);
level02.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Prefs.setStagePref(getApplicationContext(), 2);
        Intent play = new Intent(LevelActivity.this, PlayActivity.class);
        startActivity(play);
    }
});

Button level03 = (Button) findViewById(R.id.level03);
level03.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Prefs.setStagePref(getApplicationContext(), 3);
        Intent play = new Intent(LevelActivity.this, PlayActivity.class);
        startActivity(play);
    }
});

我想要制作超过20个按钮,所以如何让这段代码干掉。

由于

3 个答案:

答案 0 :(得分:2)

在方法中重复使用

private void f(int i){
        Prefs.setStagePref(getApplicationContext(), i);
        Intent play = new Intent(LevelActivity.this, PlayActivity.class);
        startActivity(play);
}

然后更改点击工作方式

布局“onClick”

   <ImageButton
                        android:layout_width="wrap_content"
                        android:layout_height="fill_parent"
                        android:id="@+id/saveButton" android:src="@android:drawable/ic_menu_save"
                        android:layout_weight="1" android:background="@drawable/menu_button" android:onClick="onClick"/>
                <ImageButton
                        android:layout_width="wrap_content"
                        android:layout_height="fill_parent"
                        android:id="@+id/wallpaperButton" android:src="@android:drawable/ic_menu_gallery"
                        android:layout_weight="1" android:background="@drawable/menu_button" android:onClick="onClick"/>
                <ImageButton
                        android:layout_width="wrap_content"
                        android:layout_height="fill_parent"
                        android:id="@+id/shareButton" android:src="@android:drawable/ic_menu_share"
                        android:layout_weight="1" android:background="@drawable/menu_button" android:onClick="onClick"
                        android:focusableInTouchMode="false"/>

活动

public void onClick(View cview) throws IOException {
    switch (cview.getId()) {
        case R.id.saveButton:
            f(1);
            break;

        case R.id.shareButton:
            f(2);
            break;

        case R.id.wallpaperButton:
            f(3);
            break;
}
}

MainActivity implements OnClickListener

...

Button level1 = (Button) findViewById(R.id.level1);
level1.setOnClickListener(this);
Button level2 = (Button) findViewById(R.id.level2);
level2.setOnClickListener(this);
Button level3 = (Button) findViewById(R.id.level3);
level3.setOnClickListener(this);
...

public void onClick(View cview) throws IOException {
    switch (cview.getId()) {
        case R.id.level1:
            f(1);
            break;

        case R.id.level2:
            f(2);
            break;

        case R.id.level3:
            f(3);
            break;
}
}

答案 1 :(得分:2)

    LinearLayout layout = (LinearLayout) findViewById(R.id.layout_buttons);
    for (int j = 0; j < 20; j++ {
        Button btn = new Button(this);
        btn.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View view) {
               Prefs.setStagePref(getApplicationContext(), j);
               Intent play = new Intent(LevelActivity.this, PlayActivity.class);
               startActivity(play);
            }
        });
        btn.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        layout.addView(btn);
    }

答案 2 :(得分:1)

您可以考虑使用butterknife

假设您正在使用gradle,只需添加

即可
compile 'com.jakewharton:butterknife:7.0.1'

到项目app /目录下build.gradle文件的dependencies部分。然后添加

ButterKnife.bind(this);

到您的Activity.onCreate方法或

ButterKnife.bind(this, view);

到onCreateView方法,如果它在Activity之外。然后,如果你稍微修改你的xml,给每个按钮一个标签:

<Button
    android:id="@+id/level01"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:tag="1"/>
<Button
    android:id="@+id/level02"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:tag="2"/>
<Button
    android:id="@+id/level03"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:tag="3"/>
然后你就可以了:

@OnClick({ R.id.level01, R.id.level02, R.id.level03 })
public void levelButtonClick(Button levelButton) {
    int value = Integer.parseInt(levelButton.getTag());
    Prefs.setStagePref(getApplicationContext(), value);
    Intent play = new Intent(LevelActivity.this, PlayActivity.class);
    startActivity(play);
}