Android 台灣中文網

標題: 求簡化JAVA程式碼(已解決) [打印本頁]

作者: moxer    時間: 2017-11-29 14:03
標題: 求簡化JAVA程式碼(已解決)
本帖最後由 moxer 於 2017-12-15 15:20 編輯

public class CHECKBOX extends AppCompatActivity {

    private CheckBox chb01,chb02,chb03,chb04,chb05,chb06,chb07,chb08,chb09,chb10;
    private CheckBox chb11,chb12,chb13,chb14,chb15,chb16,chb17,chb18,chb19,chb20;
    private Button b001;
    private TextView t001;

    @Override
     public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.
checka);
        setupViewcomponent();
    }

   
public void setupViewcomponent(){

        
chb01 =(CheckBox)findViewById(R.id.checka_chb01);
        
chb02 =(CheckBox)findViewById(R.id.checka_chb02);
        
chb03 =(CheckBox)findViewById(R.id.checka_chb03);
        
chb04 =(CheckBox)findViewById(R.id.checka_chb04);
        
chb05 =(CheckBox)findViewById(R.id.checka_chb05);
        
chb06 =(CheckBox)findViewById(R.id.checka_chb06);
        
chb07 =(CheckBox)findViewById(R.id.checka_chb07);
        
chb08 =(CheckBox)findViewById(R.id.checka_chb08);
        
chb09 =(CheckBox)findViewById(R.id.checka_chb09);
        
chb10 =(CheckBox)findViewById(R.id.checka_chb10);
        
chb11 =(CheckBox)findViewById(R.id.checka_chb11);
        
chb12 =(CheckBox)findViewById(R.id.checka_chb12);
        
chb13 =(CheckBox)findViewById(R.id.checka_chb13);
        
chb14 =(CheckBox)findViewById(R.id.checka_chb14);
        
chb15 =(CheckBox)findViewById(R.id.checka_chb15);
        
chb16 =(CheckBox)findViewById(R.id.checka_chb16);
        
chb17 =(CheckBox)findViewById(R.id.checka_chb17);
        
chb18 =(CheckBox)findViewById(R.id.checka_chb18);
        
chb19 =(CheckBox)findViewById(R.id.checka_chb19);
        
chb20 =(CheckBox)findViewById(R.id.checka_chb20);
        
b001=(Button)findViewById(R.id.checka_b001);
        
t001=(TextView)findViewById(R.id.checka_t001);

        
b001.setOnClickListener(btn01OnClick);
    }

     
public Button.OnClickListener btn01OnClick = new Button.OnClickListener() {

        
@Override
        public void onClick(View v) {
            String ans01 = getString(R.string.
checka_t001);


            
if (chb02.isChecked())
                ans01 +=
chb02.getText().toString();
            
if (chb03.isChecked())
                ans01 +=
chb03.getText().toString();
            
if (chb04.isChecked())
                ans01 +=
chb04.getText().toString();
            
if (chb05.isChecked())
                ans01 +=
chb05.getText().toString();
            
if (chb06.isChecked())
                ans01 +=
chb06.getText().toString();
            
if (chb07.isChecked())
                ans01 +=
chb07.getText().toString();
            
if (chb08.isChecked())
                ans01 +=
chb08.getText().toString();
            
if (chb09.isChecked())
                ans01 +=
chb09.getText().toString();
            
if (chb10.isChecked())
                ans01 +=
chb10.getText().toString();
            
if (chb11.isChecked())
                ans01 +=
chb11.getText().toString();
            
if (chb12.isChecked())
                ans01 +=
chb12.getText().toString();
            
if (chb13.isChecked())
                ans01 +=
chb13.getText().toString();
            
if (chb14.isChecked())
                ans01 +=
chb14.getText().toString();
            
if (chb15.isChecked())
                ans01 +=
chb15.getText().toString();
            
if (chb16.isChecked())
                ans01 +=
chb16.getText().toString();
            
if (chb17.isChecked())
                ans01 +=
chb17.getText().toString();
            
if (chb18.isChecked())
                ans01 +=
chb18.getText().toString();
            
if (chb19.isChecked())
                ans01 +=
chb19.getText().toString();
            
if (chb20.isChecked())
                ans01 +=
chb20.getText().toString();
            
t001.setText(ans01);
        }
    };
}


求如何簡化以上程式碼?

作者: muska1116    時間: 2017-12-1 20:35
本帖最後由 muska1116 於 2017-12-1 20:47 編輯

public Button.OnClickListener btn01OnClick = new Button.OnClickListener() {

        @Override
        public void onClick(View v) {
            for (int i = 1; i < 21; i++) {
                String idName = "checka_chb" + String.format("%02d", i);
                int resID = getResources().getIdentifier(idName, "id", getPackageName());

                CheckBox checkBox = (CheckBox) findViewById(resID);
                if (checkBox.isChecked()) {
                    ans01 += checkBox.getText().toString();
                }
            }
            t001.setText(ans01);
        }
};

上面的 CheckBox 就可以不用宣告了,這個作法不考慮效能…  
好一點的做法其實可以用 List<View> 來保存 checkBox,按下按鈕後再從 List 裡面取值…
只是你要求簡短,這應該夠短了XD



作者: moxer    時間: 2017-12-2 20:03
muska1116 發表於 2017-12-1 20:35
public Button.OnClickListener btn01OnClick = new Button.OnClickListener() {

        @Override

原來是用getIdentifier來取得我的ID群
在看到您的回復之前,我還在想用陣列Array來解決
可是一直卡在Id這部分,有想到轉String[],卻Run不過去
原來是要在按鈕監聽內做

很幸運能得到高手的協助,一次讓我學會好多方法
你還刻意不把我那行沒改到ID的宣告給打出來,非常感謝
被老師抓到也是只能認了...
不過重要的是我從這裡學會的方法,後面一定會勤加練習使用
感謝





歡迎光臨 Android 台灣中文網 (https://apk.tw/) Powered by Discuz! X3.1