Fajar Khan Fajar Khan - 4 months ago 12
Java Question

operators cannot be applied to a float,'android.widget.EditText'

I am making a simple calculator and for the -/+ button i write the code bellow:

float ab=-1;
this.txtScreen = (EditText) findViewById(R.id.txtScreen);
unary=(Button)findViewById(R.id.plusminus);
mod=(Button)findViewById(R.id.modul);

unary.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
txtScreen.setText(ab*txtScreen);
}
});


and showing me error * operator cannot applied to float.

Answer

Try this (it needs string):

    float ab=-1;


    unary=(Button)findViewById(R.id.plusminus); 
    mod=(Button)findViewById(R.id.modul); 

    unary.setOnClickListener(new View.OnClickListener() {
            @Override 
            public void onClick(View v) {
                txtScreen.setText(String.valueOf(ab * Float.valueOf(txtScreen.getText().toString())));
            } 
        }); 

Edit: Getting the value from the txtScreen in calculation.