Kerv
Kerv

Reputation: 179

Disabling edittext by using a checkbox error

I have a problem here. I want to disable a edittext by using a checkbox. If checkbox is checked, edittext isa available, if not, it is disabled. But I'm having problems. Here is my code: Can anyone check on this. Accdng to eclipse, no error. But when I run it on my phone, the enabling/disabling is not functioning.

public class Order extends Activity {  
private Button button1;  
private EditText txtbox1,txtbox2;  
private TextView tv;
CheckBox check1;

/** Called when the activity is first created. */  
@Override  
public void onCreate(Bundle savedInstanceState) {  
    super.onCreate(savedInstanceState);  
    setContentView(R.layout.order);  
    txtbox1= (EditText) findViewById(R.id.editText1);  
    button1 = (Button) findViewById(R.id.button1);  
    tv = (TextView) findViewById(R.id.editText5);  
    txtbox2= (EditText) findViewById(R.id.editText2);
    check1 = (CheckBox)findViewById(R.id.checkcheck);
    button1.setOnClickListener(new clicker());


}
public void addListenerOncheck1() {

    check1.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // TODO Auto-generated method stub
                if(isChecked){
                    txtbox1.setFocusable(true);
        txtbox1.setFocusableInTouchMode(true);
                } else {
                    txtbox1.setFocusable(false);
        txtbox1.setFocusableInTouchMode(false);
                }

            }
        });
  }

class clicker implements Button.OnClickListener  
{  
    public void onClick(View v)  
    {  
        String a,b;  
        Integer vis;  
        a = txtbox1.getText().toString();  
        b = txtbox2.getText().toString();  
        vis = Integer.parseInt(a)*2+Integer.parseInt(b)*3;  
        tv.setText(vis.toString());  
    }  
}  

}

Upvotes: 0

Views: 921

Answers (1)

MojioMS
MojioMS

Reputation: 1693

you are nowhere adding your Listener by calling ur method addListenerOncheck1(); so put addListenerOncheck1(); add the end of you OnCreate method.

But I highly recommend you not to use all these additional self-made Classes for just adding listeners. Use the following code instead and add it into your OnCreate Method:

@Override  
public void onCreate(Bundle savedInstanceState) {  
    super.onCreate(savedInstanceState);  
    setContentView(R.layout.order);  
    txtbox1= (EditText) findViewById(R.id.editText1);  
    button1 = (Button) findViewById(R.id.button1);  
    tv = (TextView) findViewById(R.id.editText5);  
    txtbox2= (EditText) findViewById(R.id.editText2);
    check1 = (CheckBox)findViewById(R.id.checkcheck);
    button1.setOnClickListener(new clicker());
    check1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView,
            boolean isChecked) {
            if (isChecked) {
                txtbox1.setEnabled(false);
            } else {
                txtbox1.setEnabled(true);
            }
        }
    });

    button1.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View v) {
            String a,b;  
            Integer vis;
            a = txtbox1.getText().toString();  
            b = txtbox2.getText().toString();  
            vis = Integer.parseInt(a)*2+Integer.parseInt(b)*3;  
            tv.setText(vis.toString());  
        }         
    });

    // This will make sure the user can only type numbers into the EditTexts:
    txtbox1.setInputType(InputType.TYPE_CLASS_NUMBER);
    txtbox2.setInputType(InputType.TYPE_CLASS_NUMBER);

    // Code to disable editTexts at start once:
    txtbox1.setEnabled(false);
    txtbox2.setEnabled(false);
}

Upvotes: 1

Related Questions