Question

    et1 = (EditText) findViewById(R.id.et1);
    et2 = (EditText) findViewById(R.id.et2);

    floatET1 = Float.parseFloat(et1.getText().toString());
    floatET2 = Float.parseFloat(et2.getText().toString());

    et1.setOnFocusChangeListener(new OnFocusChangeListener() {

        public void onFocusChange(View v, boolean hasFocus) {
            if (hasFocus) {
                float sonuc1 = floatET2 * 60;
                et2.setText(sonuc1);
            }
        }
    });

    et2.setOnFocusChangeListener(new OnFocusChangeListener() {

        public void onFocusChange(View v, boolean hasFocus) {
            if (hasFocus) {
                float sonuc2 = floatET1  / 60;
                et1.setText(sonuc2);

when i click EditText (et2) then i want to calculating to minute and when i click to EditText (et1) then convert to hour... but i doesn't work :/

Was it helpful?

Solution

This will do what you want, although you probably want to inform the user with a toast message or something rather than quietly doing nothing when the input values aren't valid strings.

et1 = (EditText) findViewById(R.id.et1);
et2 = (EditText) findViewById(R.id.et2);

et1.setOnFocusChangeListener(new OnFocusChangeListener() {

    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus) {
            try {
                floatET2 = Float.parseFloat(et2.getText().toString());
            } catch (NumberFormatException e) {
            }
            float sonuc1 = floatET2 * 60;
            et1.setText(String.valueOf(sonuc1));
        }
    }
});

et2.setOnFocusChangeListener(new OnFocusChangeListener() {

    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus) {
            try {
                floatET1 = Float.parseFloat(et1.getText().toString());
            } catch (NumberFormatException e) {
            }
            float sonuc2 = floatET1 / 60;
            et2.setText(String.valueOf(sonuc2));
        }
    }
});

OTHER TIPS

You're getting your edittext content too early. Move the part involving getText() to the focus change listener.

You need to check input type of edittext. If user enters value like 1.6, then your code is not working. So make sure to provide input type then you can right your logic to calculate minutes.

You need move:

 floatET1 = Float.parseFloat(et1.getText().toString());
 floatET2 = Float.parseFloat(et2.getText().toString());

into every

public void onFocusChange(View v, boolean hasFocus) {

}

Because you need get the text in every focus change.

Then you should convert the result of operation into String (you are trying set a float value in setTex() method):

et1.setText(""+sonuc2);
et2.setText(""+sonuc1);

or

et1.setText(sonuc2.toString());
et2.setText(sonuc1.toString());
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top