简体   繁体   中英

TextWatchers won't work

I'm really really desperate

I'm forced to work on an Android App, but my code won't

got three text watchers, one to add a new row to a table as soon as a character is inserted in the first row

a second to check a time format

the third to make a decimal time using endtime-starttime

the first works, the second and third don't

MainActivity public class MainActivity extends AppCompatActivity {

public TableRow lastRow;
public newRowTextWatcher tw;{
    tw= new newRowTextWatcher(this);}
public Sum sum;{
    sum= new Sum(this);}
public Time24hFormatValidator time24;{
    time24 = new Time24hFormatValidator(this );}  
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    tw = new newRowTextWatcher(this);
    time24 = new Time24hFormatValidator(this);

    lastRow = (TableRow) findViewById(R.id.firstRow);


    EditText txt1 = (EditText) findViewById(R.id.etOrderNo);
    EditText txt2 = (EditText) findViewById(R.id.etMachineID);
    EditText txt3 = (EditText) findViewById(R.id.etstarttime);
    EditText txt4 = (EditText) findViewById(R.id.etendtime);
    EditText txt5 = (EditText) findViewById(R.id.etdecimaltime);
    EditText txt6 = (EditText) findViewById(R.id.etrework);
    EditText txt7 = (EditText) findViewById(R.id.etstatus);

    txt3.addTextChangedListener(time24);
    txt4.addTextChangedListener(time24);

    txt1.addTextChangedListener(tw);
    txt2.addTextChangedListener(tw);
    txt3.addTextChangedListener(tw);
    txt4.addTextChangedListener(tw);
    txt5.addTextChangedListener(tw);
    txt6.addTextChangedListener(tw);
    txt7.addTextChangedListener(tw);

newrowtextwatcher

public class newRowTextWatcher implements TextWatcher {
MainActivity ze;

public newRowTextWatcher(MainActivity Zeiterfassung) {
    ze = Zeiterfassung;
}

public void afterTextChanged(Editable editable) {

    EditText txt1 = (EditText) ze.lastRow.getChildAt(0);
    EditText txt2 = (EditText) ze.lastRow.getChildAt(1);
    EditText txt3 = (EditText) ze.lastRow.getChildAt(2);
    EditText txt4 = (EditText) ze.lastRow.getChildAt(3);
    EditText txt5 = (EditText) ze.lastRow.getChildAt(4);
    EditText txt6 = (EditText) ze.lastRow.getChildAt(5);
    EditText txt7 = (EditText) ze.lastRow.getChildAt(6);

    if (txt1.getText().toString().equals("") &&
            txt2.getText().toString().equals("") &&
            txt3.getText().toString().equals("") &&
            txt4.getText().toString().equals("") &&
            txt5.getText().toString().equals("") &&
            txt6.getText().toString().equals("") &&
            txt7.getText().toString().equals(""))
    {
        return;
    }
    else
        ze.AddRow();

validator

public class Time24hFormatValidator implements TextWatcher {
MainActivity ze;

public Time24hFormatValidator(MainActivity Zeiterfassung) {
    ze = Zeiterfassung;
}
public void afterTextChanged(Editable editable) {
    EditText txt3 = (EditText) ze.lastRow.getChildAt(3);
    EditText txt4 = (EditText) ze.lastRow.getChildAt(4);


    String str = txt3.getText().toString();
    if(!str.equals(""))
    {
        String[] parts = str.split(":");
        String part1 = parts[0];
        String part2 = parts[1];
        int hours = Integer.parseInt(part1);
        int minutes= Integer.parseInt(part2);

        if (hours>=0 || hours<24)
        {
            return;
        }
        else
            txt3.setText("");
        Toast.makeText(ze.getApplicationContext(), "Wrong", Toast.LENGTH_LONG).show();


        if (minutes>=0 || minutes<60)
        {
            return;
        } else
            txt3.setText("");
        Toast.makeText(ze.getApplicationContext(), "Wrong", Toast.LENGTH_LONG).show();

    }
}}

please help

This

public void afterTextChanged(Editable editable)

is defined as regular void, try to put @Override over it like following, so it will know that you mean the void from class you've implemented.

@Override
public void afterTextChanged(Editable s) {

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM