Android 如何简化不使用if语句的方法

Android 如何简化不使用if语句的方法,android,if-statement,Android,If Statement,如何简化不使用if语句的方法并减少代码行数。我是这样写的: private void RemoveImg() { while (true) { rel_with_images.getChildCount(); if ((number_of_Image== 5) { rel_with_images.removeViewAt(number_of_Image- 1);

如何简化不使用if语句的方法并减少代码行数。我是这样写的:

    private void RemoveImg() {
        while (true) {
            rel_with_images.getChildCount();

            if ((number_of_Image== 5) {
                rel_with_images.removeViewAt(number_of_Image- 1);
                sp_1 = sp11.play(sp_1 , 1.0F, 1.0F, 0, 0, 1.0F);
                inta11 = sp11.play(intaa, 1.0F, 1.0F, 0, 0, 1.0F);
                CheckVibrate();
                MainAct c5 = MainAct .this;
                c5.number_of_Image = (-1 + c5.number_of_Image);
                break;
            }
            if ((number_of_Image== 4) {
                rel_with_images.removeViewAt(number_of_Image- 1);
                sp_1 = sp11.play(sp_1 , 1.0F, 1.0F, 0, 0, 1.0F);
                inta11 = sp11.play(intaa, 1.0F, 1.0F, 0, 0, 1.0F);
                CheckVibrate();
                MainAct c5 = MainAct .this;
                c5.number_of_Image = (-1 + c5.number_of_Image);
                break;
            }
            if ((number_of_Image== 3 ) {
                rel_with_images.removeViewAt(number_of_Image- 1);
                sp_1 = sp11.play(sp_1 , 1.0F, 1.0F, 0, 0, 1.0F);
                inta11 = sp11.play(intaa, 1.0F, 1.0F, 0, 0, 1.0F);
                CheckVibrate();
                MainAct c5 = MainAct .this;
                c5.number_of_Image = (-1 + c5.number_of_Image);
                break;
            }
            if ((number_of_Image== 2 ) {
                rel_with_images.removeViewAt(number_of_Image- 1);
                sp_1 = sp11.play(sp_1 , 1.0F, 1.0F, 0, 0, 1.0F);
                inta11 = sp11.play(intaa, 1.0F, 1.0F, 0, 0, 1.0F);
                CheckVibrate();
                MainAct c5 = MainAct .this;
                c5.number_of_Image = (-1 + c5.number_of_Image);
                break;
            }
            if ((number_of_Image== 1) {
                rel_with_images.removeViewAt(number_of_Image- 1);
                sp_1 = sp11.play(sp_1 , 1.0F, 1.0F, 0, 0, 1.0F);
                inta11 = sp11.play(intaa, 1.0F, 1.0F, 0, 0, 1.0F);
                CheckVibrate();
                MainAct c5 = MainAct .this;
                c5.number_of_Image = (-1 + c5.number_of_Image);
                break;
            }
       }
}

number_of_Image是一个整数,它统计与_图像相关的相对布局中的图像数。当版面中有30多张图片时,代码有点大。有人知道解决办法吗?谢谢

注意,我对
removeImageAt(int)
的内容能否正常工作/编译没有多少信心-这只是对您原来问题的重构。谢谢您的回复。您的代码工作正常,我刚刚测试了它,但从布局中删除了所有视图。方法RemoveImg每个用户单击只删除一个图像。我修复了它现在可以工作了。我在(numberOfImages>0){removeImageAt(number_of_Image-1)numberOfImages=rel_with_images.getChildCount();break;}谢谢啊,是的,对不起,我错过了你的代码中的
break
——我的示例故意删除了所有视图,因为我认为这就是你想要做的。Np:)我从这篇文章中学到了很多。谢谢你为什么要用if语句开始呢?据我所知,每个if语句中的代码都是相同的。毫无意义的if语句(除非我读错了),只需删除它。
private void removeAllImages() {
    int numberOfImages = rel_with_images.getChildCount();

    while (numberOfImages > 0) {
        removeImageAt(number_of_Image-1)
        numberOfImages = rel_with_images.getChildCount();
    }
}

private void removeImageAt(int position) {
    rel_with_images.removeViewAt(position);
    sp_1 = sp11.play(sp_1 , 1.0F, 1.0F, 0, 0, 1.0F);
    inta11 = sp11.play(intaa, 1.0F, 1.0F, 0, 0, 1.0F);
    CheckVibrate();
    MainAct c5 = MainAct .this;
    c5.number_of_Image--;
}