java – Android:制作简单的代码.干

我是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个按钮,那么如何使这段代码干掉.

谢谢

解决方法:

在方法中重复功能

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;
}
}
上一篇:java day42【综合练习】


下一篇:java 后端请求第三方接口 包含post请求和get请求