Android : onCheckedChanged with multiple setOnFocusChangedListener

on Tuesday, August 12, 2014


I have 2 different custom setOnFocusChangedListener method, namely large and normal. After setting them up. I call it depending on my toggleButton state.


The thing right now is now I'm currently in Large Mode which is represents isChecked.


So for example, my current focus is at Row 1 of my EditText, when I click the ToggleButton, it goes to the normal setOnFocusChangedListener, which then will run the switch case. Since I'm on Row 1, my Row 2 EditText should be enabled and the hint will be set as stated. But instead of that, I have to change to another View(either click on row3TextField or row4TextField) in order to allow my Row 2 EditText to show the hint and enable it.


Shouldn't it be enabled immediately when the button state is changed from isChecked to !isChecked?


I put toggleButton.setOnCheckedChangedListener(this); in my onCreate();


For Large method, the code is;



OnFocusChangeListener large = new OnFocusChangeListener() {

@Override
public void onFocusChange(View v, boolean hasFocus) {
String row1TextContains = row1TextField.getText().toString().trim();
String row2TextContains = row2TextField.getText().toString().trim();
String row3TextContains = row3TextField.getText().toString().trim();
String row4TextContains = row4TextField.getText().toString().trim();
switch (v.getId()) {
case R.id.row1:

if (row2TextContains.isEmpty()) {
row3TextField.setEnabled(true);
row3TextField.setHint(row3Hint);
e = 0;
}
if (row3TextContains.isEmpty()) {
row4TextField.setEnabled(true);
row4TextField.setHint(row4Hint);
g = 0;
}

b = 1; // Large Mode
row2TextField.setEnabled(false);
row2TextField.setHint("Disabled");
c = 1; // Row 2 Disable

break;
case R.id.row2:

if (c == 0) {

if (row3TextContains.isEmpty()) {
row4TextField.setEnabled(true);
row4TextField.setHint(row4Hint);
g = 0;
}
d = 1;
row3TextField.setEnabled(false);
row3TextField.setHint("Disabled");
e = 1;

}

break;

case R.id.row3:

if (e == 0) {

if (row1TextContains.isEmpty()) {
row2TextField.setEnabled(true);
row2TextField.setHint(row2Hint);
c = 0;
}
f = 1;
row4TextField.setEnabled(false);
row4TextField.setHint("Disabled");
g = 1;

}

break;

case R.id.row4:

Toast toast = Toast.makeText(getApplicationContext(),
"Row 4 of Large Character is not allowed",
Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
row4TextField.setHint("");
row1TextField.requestFocus();

break;
}

}
};


For Normal method, the code is:



OnFocusChangeListener normal = new OnFocusChangeListener() {

@Override
public void onFocusChange(View v, boolean hasFocus) {
switch (v.getId()) {
case R.id.row1:
c = 0;
row2TextField.setEnabled(true);
row2TextField.setHint(row2Hint);
break;

case R.id.row2:
e = 0;
row3TextField.setEnabled(true);
row3TextField.setHint(row3Hint);
break;

case R.id.row3:
g = 0;
row4TextField.setEnabled(true);
row4TextField.setHint(row4Hint);
break;

}
}
};


ToggleButton setOnCheckedChanged Method:



@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
String row1TextContains = row1TextField.getText().toString().trim();
String row2TextContains = row2TextField.getText().toString().trim();
String row3TextContains = row3TextField.getText().toString().trim();
String row4TextContains = row4TextField.getText().toString().trim();
if (isChecked) {
row1TextField.setOnFocusChangeListener(large);
row2TextField.setOnFocusChangeListener(large);
row3TextField.setOnFocusChangeListener(large);
row4TextField.setOnFocusChangeListener(large);
} else {
row1TextField.setOnFocusChangeListener(normal);
row2TextField.setOnFocusChangeListener(normal);
row3TextField.setOnFocusChangeListener(normal);
row4TextField.setOnFocusChangeListener(normal);
}

}

0 comments:

Post a Comment