多个addTextChangedListeners,无需复制代码

时间:2016-11-09 20:38:49

标签: java android textchanged

我目前在其中一个屏幕上有24 <!DOCTYPE html> <html> <head> <title>Vinn en Bil!</title> <link rel="shortcut icon" href="volvo.ico"></link> <link rel="stylesheet" type="text/css" href="form.css"> <meta charset="UFT-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> </head> <body> <div id="maincontainer"> <div id="topsection"> <div class="innertube"> <h1> Vinn en bil </h1> <hr> </div> </div> <div id="contentwrapper"> <div id="contentcolumn"> <div class="innertube"> <br> <form> <fieldset class="fieldset"> <legend></legend> <h2>Personlig Information</h2> <br> <label for="namn">Förnamn:</label> <input type="text" placeholder="Förnamn" onfocus="this.placeholder = ''" onblur="this.placeholder = 'Förnamn'" required /> <br> <br> <label for="efternamn">Efternamn:</label> <input type="text" placeholder="Efternamn" onfocus="this.placeholder = ''" onblur="this.placeholder = 'Efternamn'" required /> <br> <br> <label for="age">Ålder: </label> <select name="age"> <option value="18-25">18-25</option> <option value="25-35">25-35</option> <option value="35-50">35-50</option> <option value="50+">50+</option> </select> <br> <br> <label for="kön">Kön:</label> <table> <tr> <td>Man</td> <td><input class="radio" type="radio"></input></td> </tr> <tr> <td>Kvinna</td> <td><input class="radio" type="radio"></input></td> </tr> <tr> <td>Annat</td> <td><input class="radio" type="radio"></input></td> </tr> </table> <br> <label for="mail">Email:</label> <input type="email" placeholder="Email" onfocus="this.placeholder = ''" onblur="this.placeholder = 'Email'" required/> <br> <br> <label for="adress">Adress:</label> <input type="text" placeholder="Adress" onfocus="this.placeholder = ''" onblur="this.placeholder = 'Adress'"/> <br> <br> <label for="postnummer">Postnummer:</label> <input type="text" placeholder="Postnummer" onfocus="this.placeholder = ''" onblur="this.placeholder = 'Postnummer'"/> <br> <br> <label for="postort">Postort:</label> <input type="text" placeholder="Postort" onfocus="this.placeholder = ''" onblur="this.placeholder = 'Postort'"/> <br> <br> <label for="nummer">Telefon nummer:</label> <input type="text" placeholder="Telefon nummer" onfocus="this.placeholder = ''" onblur="this.placeholder = 'Telefon nummer'"/> <br> </fieldset> <br> <br> <fieldset class="fieldset"> <legend></legend> <h2>Om du vann?</h2> <table> <tr> <td>Välj en bil</td> <td> <div class="form-group"> <select name="make" id="make"> <option value="">Välj en Volvo typ</option> <option value="XC">XC</option> <option value="V">V</option> <option value="S">S</option> <option value="Laddhybrider">Laddhybrider</option> </select> </div> </td> <td> <div class="from-group"> <select name="type" id="type"> <option value="" class="">Välj en Volvo model </option> <option value="XC90" class="XC">XC90</option> <option value="XC60" class="XC">XC60</option> <option value="V60 Cross country" class="V">V90 Cross country</option> <option value="V90" class="V">V90</option> <option value="V60 Cross country" class="V">V60 Cross country</option> <option value="V60" class="V">V60</option> <option value="V40 Cross country" class="V">V40 Cross country</option> <option value="V40" class="V">V40</option> <option value="S90" class="S">S90</option> <option value="S60 Cross country" class="S">S60 Cross country</option> <option value="S60" class="S">S60</option> <option value="XC90 T8 Twin engine" class="Laddhybrider">XC90 T8 Twin engine</option> <option value="V60 D6 Twin engine" class="Laddhybrider">V60 D6 Twin engine</option> <option value="V60 D5 Twin engine" class="Laddhybrider">V60 D5 Twin engine</option> </select> </div> </td> </tr> </table> <br> <p>Varför ska just du ska vinna:</p> <textarea rows="20" cols="60"></textarea> <br> <br> </fieldset> <br> <br> <fieldset class="fieldset"> <legend></legend> <h2>Omdöme på hemsidan</h2> <br> <p>Vad tycker du om sidan</p> <textarea rows="20" cols="60"></textarea> <input type="submit" value="Klar" onclick="alert('Tack för ditt deltagande!')"> <input type="reset"> </form> </div> </div> </div> <div id="footer"><a href="http://www.volvocars.com/se?gclid=Cj0KEQjwqMHABRDVl6_hqKGDyNIBEiQAN-O9hMtUPMIE4aak0QmcGyXpDihhM-HkAJVCuGxwqf7fpQUaAmVb8P8HAQ" target="_blank">Volvo</a></div> </div> </body> </html>个,我需要听众在其中一个更改时更新总计,但我不确定如何设置它为听众提供24个独立的代码片段。答案可能非常明显,但我无法弄明白,我发现了一些类似的问题但却无法自己解决问题。

这是我的代码:

EditText

就目前而言,我认为我必须让24位那些我认为并不是很好的方式的听众。

1 个答案:

答案 0 :(得分:0)

对于这种特定情况,为什么没有一个采用EditText并对其应用TextWatcher的方法? e.g。

protected void applyTextWatcher(EditText roundInput, TextView boxerTotalText, EditText[] roundInputList){
    roundInput.addTextChangedListener(new TextWatcher() {
        ....
        @Override
        public void afterTextChanged(Editable editable) {
          boxerTotalText.setText(String.valueOf(sumRounds(roundInputList)));
        }
    });
}

您可以通过以下方式使用:

for(EditText roundInput : boxer1List)
    applyTextWatcher(roundInput, totalBoxer1Text, boxer1List);

虽然,你仍然会在这里复制很多代码,但它不是很灵活(即更多/更少的轮次意味着部署代码更改),你可能想要更加面向对象做法。 e.g。

您可以拥有

,而不是没有真正参考的EditTexts列表
public class Round {
    int roundNumber;

    int boxer1Score;
    int boxer2Score;
}

public class RoundViewHolder {
    Round round;

    EditText boxer1Input;
    EditText boxer2Input;
}

然后,您将使用List<RoundViewHolder>而不是仅使用EditTexts,它直接链接到Round,这将根据实际发生的轮数帮助您动态生成EditTexts

这是一些伪代码而不是你需要的一切,但这应该指向正确的方向。