bkta
bkta

Reputation: 15

android convert hour to minute

    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 :/

Upvotes: 1

Views: 1586

Answers (4)

RdlP
RdlP

Reputation: 1396

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());

Upvotes: 0

Shrikant Salunkhe
Shrikant Salunkhe

Reputation: 339

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.

Upvotes: 0

scottt
scottt

Reputation: 8371

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));
        }
    }
});

Upvotes: 1

laalto
laalto

Reputation: 152867

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

Upvotes: 1

Related Questions