mardi 30 juin 2015

How to write if else to get value from edit text android

I have this 3 types of edit text input. I want to compare the 3 edit text to get the FreeTime.

xml codes

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="@string/morningLabel"
    android:id="@+id/MorningLabel"
    android:textStyle="bold"
    android:layout_alignTop="@+id/textView2"
    android:layout_alignRight="@+id/MorningText"
    android:layout_alignEnd="@+id/MorningText" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Working Hour(s)"
    android:id="@+id/textView3"
    android:layout_centerHorizontal="true"
    android:textStyle="bold" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Afternoon"
    android:id="@+id/textView2"
    android:textStyle="bold"
    android:layout_alignTop="@+id/textView5"
    android:layout_centerHorizontal="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Evening"
    android:id="@+id/textView5"
    android:textStyle="bold"
    android:layout_marginBottom="41dp"
    android:layout_above="@+id/AfternoonText"
    android:layout_toRightOf="@+id/textView3"
    android:layout_toEndOf="@+id/textView3" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:text=" 6.01 a.m. \n       to \n12.00 p.m."
    android:id="@+id/morningTime"
    android:textStyle="italic"
    android:layout_centerVertical="true"
    android:layout_toLeftOf="@+id/textView3"
    android:layout_toStartOf="@+id/textView3" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:text=" 12.01 p.m. \n       to \n 6.00 p.m."
    android:id="@+id/afternoonTime"
    android:textStyle="italic"
    android:layout_alignTop="@+id/morningTime"
    android:layout_alignLeft="@+id/textView2"
    android:layout_alignStart="@+id/textView2" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:text=" 6.01 p.m. \n       to \n12.00 a.m."
    android:id="@+id/eveningTime"
    android:textStyle="italic"
    android:layout_alignTop="@+id/afternoonTime"
    android:layout_alignLeft="@+id/EveningText"
    android:layout_alignStart="@+id/EveningText" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Result"
    android:id="@+id/btnNext"
    android:layout_alignParentBottom="true"
    android:layout_marginBottom="38dp"
    android:layout_alignRight="@+id/eveningTime"
    android:layout_alignEnd="@+id/eveningTime"
    android:layout_alignLeft="@+id/morningTime"
    android:layout_alignStart="@+id/morningTime"
    android:textStyle="bold" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="number"
    android:ems="10"
    android:id="@+id/MorningText"
    android:hint="0-6"
    android:layout_marginBottom="22dp"
    android:autoText="true"
    android:background="#ffd1d1d1"
    android:editable="false"
    android:textSize="@dimen/abc_text_size_display_1_material"
    android:layout_above="@+id/morningTime"
    android:layout_toLeftOf="@+id/textView3"
    android:layout_alignLeft="@+id/morningTime"
    android:layout_alignStart="@+id/morningTime"
    android:text="@string/MorningInput"
    android:singleLine="false"
    android:textAlignment="center"
    android:numeric="integer" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="number"
    android:ems="10"
    android:id="@+id/AfternoonText"
    android:hint="0-6"
    android:autoText="true"
    android:background="#ffd1d1d1"
    android:editable="false"
    android:textSize="@dimen/abc_text_size_display_1_material"
    android:layout_alignTop="@+id/MorningText"
    android:layout_alignLeft="@+id/afternoonTime"
    android:layout_alignStart="@+id/afternoonTime"
    android:layout_alignRight="@+id/afternoonTime"
    android:layout_alignEnd="@+id/afternoonTime"
    android:text="@string/AfternoonInput"
    android:textAlignment="center"
    android:numeric="integer" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="number"
    android:ems="10"
    android:id="@+id/EveningText"
    android:hint="0-6"
    android:autoText="true"
    android:background="#ffd1d1d1"
    android:editable="false"
    android:textSize="@dimen/abc_text_size_display_1_material"
    android:layout_alignBaseline="@+id/AfternoonText"
    android:layout_alignBottom="@+id/AfternoonText"
    android:layout_alignRight="@+id/textView5"
    android:layout_alignEnd="@+id/textView5"
    android:text="@string/EveningInput"
    android:textAlignment="center"
    android:numeric="integer"
    android:layout_alignLeft="@+id/textView5"
    android:layout_alignStart="@+id/textView5" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:text="@string/timeLabel"
    android:id="@+id/TimeText"
    android:layout_above="@+id/btnNext"
    android:layout_alignLeft="@+id/btnNext"
    android:layout_alignStart="@+id/btnNext" />

<Button
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="c"
    android:onClick="calculateClickHandler"
    android:id="@+id/btnc"
    android:layout_above="@+id/TimeText"
    android:layout_toRightOf="@+id/afternoonTime"
    android:layout_toEndOf="@+id/afternoonTime"
    android:layout_marginBottom="42dp" />

this is the java codes. I think I put the wrong if else. How can I solve this? Anyone help~

public class WorkingHour extends Activity {

public void calculateClickHandler(View view)
{

    if (view.getId() == R.id.btnc)
    {
        Intent i=new Intent(this, Result.class);

        // get the references to the widgets
        EditText MorningInput = (EditText)findViewById(R.id.MorningText);
        EditText AfternoonInput = (EditText)findViewById(R.id.AfternoonText);
        EditText EveningInput = (EditText)findViewById(R.id.EveningText);
        TextView TimeText = (TextView)findViewById(R.id.TimeText);

        // get the users values from the widget references
        int morninghour = Integer.parseInt(MorningInput.getText().toString());
        int afternoonhour = Integer.parseInt(AfternoonInput.getText().toString());
        int eveninghour = Integer.parseInt(EveningInput.getText().toString());

        // calculate the free time
        int freeTimeValue = calculateFreeTime(morninghour,afternoonhour,eveninghour);

        // interpret the meaning of the bmi value
        String freetimeInterpretation = interpretFT(freeTimeValue);

        // now set the value in the result text
        TimeText.setText(freeTimeValue + "-" + freetimeInterpretation);

        SharedPreferences sp2 = getSharedPreferences("name2",MODE_PRIVATE);
        SharedPreferences.Editor editor=sp2.edit();
        editor.putString("rslt 3",TimeText.getText().toString());
        editor.commit();
    }
}

// the formula to calculate the free time
private int calculateFreeTime (int morninghour, int afternoonhour, int eveninghour)
{
    if (((6- morninghour)> (6-afternoonhour)) && ((6- morninghour)> (6-eveninghour)))
    {
        return (int) (6 - morninghour);
    }
    else if (((6- afternoonhour)> (6-morninghour)) && ((6- afternoonhour)> (6-eveninghour)))
    {
        return (int) (6 - afternoonhour);
    }
    else if (((6- eveninghour)> (6-morninghour)) && ((6-eveninghour)> (6-afternoonhour)))
    {
        return (int) (6 - eveninghour);
    }
    return 0;
}

// interpret what free time

private String interpretFT(int freetime)
{
    if (freetime < )
    {
        return "morning";
    }
    else if (freetime < )
    {
        return "Afternoon";
    }

}

Aucun commentaire:

Enregistrer un commentaire