java如何编写if-else以从编辑文本中获取值
我有这3种类型的编辑文本输入
我想比较3个编辑文本以得到FreeTime
xml代码
<TextView
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:textAppearance="?安卓:attr/textAppearanceMedium"
安卓:text="@string/morningLabel"
安卓:id="@+id/MorningLabel"
安卓:textStyle="bold"
安卓:layout_alignTop="@+id/textView2"
安卓:layout_alignRight="@+id/MorningText"
安卓:layout_alignEnd="@+id/MorningText" />
<TextView
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:textAppearance="?安卓:attr/textAppearanceLarge"
安卓:text="Working Hour(s)"
安卓:id="@+id/textView3"
安卓:layout_centerHorizontal="true"
安卓:textStyle="bold" />
<TextView
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:textAppearance="?安卓:attr/textAppearanceMedium"
安卓:text="Afternoon"
安卓:id="@+id/textView2"
安卓:textStyle="bold"
安卓:layout_alignTop="@+id/textView5"
安卓:layout_centerHorizontal="true" />
<TextView
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:textAppearance="?安卓:attr/textAppearanceMedium"
安卓:text="Evening"
安卓:id="@+id/textView5"
安卓:textStyle="bold"
安卓:layout_marginBottom="41dp"
安卓:layout_above="@+id/AfternoonText"
安卓:layout_toRightOf="@+id/textView3"
安卓:layout_toEndOf="@+id/textView3" />
<TextView
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:textAppearance="?安卓:attr/textAppearanceSmall"
安卓:text=" 6.01 a.m. \n to \n12.00 p.m."
安卓:id="@+id/morningTime"
安卓:textStyle="italic"
安卓:layout_centerVertical="true"
安卓:layout_toLeftOf="@+id/textView3"
安卓:layout_toStartOf="@+id/textView3" />
<TextView
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:textAppearance="?安卓:attr/textAppearanceSmall"
安卓:text=" 12.01 p.m. \n to \n 6.00 p.m."
安卓:id="@+id/afternoonTime"
安卓:textStyle="italic"
安卓:layout_alignTop="@+id/morningTime"
安卓:layout_alignLeft="@+id/textView2"
安卓:layout_alignStart="@+id/textView2" />
<TextView
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:textAppearance="?安卓:attr/textAppearanceSmall"
安卓:text=" 6.01 p.m. \n to \n12.00 a.m."
安卓:id="@+id/eveningTime"
安卓:textStyle="italic"
安卓:layout_alignTop="@+id/afternoonTime"
安卓:layout_alignLeft="@+id/EveningText"
安卓:layout_alignStart="@+id/EveningText" />
<Button
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:text="Result"
安卓:id="@+id/btnNext"
安卓:layout_alignParentBottom="true"
安卓:layout_marginBottom="38dp"
安卓:layout_alignRight="@+id/eveningTime"
安卓:layout_alignEnd="@+id/eveningTime"
安卓:layout_alignLeft="@+id/morningTime"
安卓:layout_alignStart="@+id/morningTime"
安卓:textStyle="bold" />
<EditText
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:inputType="number"
安卓:ems="10"
安卓:id="@+id/MorningText"
安卓:hint="0-6"
安卓:layout_marginBottom="22dp"
安卓:autoText="true"
安卓:background="#ffd1d1d1"
安卓:editable="false"
安卓:textSize="@dimen/abc_text_size_display_1_material"
安卓:layout_above="@+id/morningTime"
安卓:layout_toLeftOf="@+id/textView3"
安卓:layout_alignLeft="@+id/morningTime"
安卓:layout_alignStart="@+id/morningTime"
安卓:text="@string/MorningInput"
安卓:singleLine="false"
安卓:textAlignment="center"
安卓:numeric="integer" />
<EditText
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:inputType="number"
安卓:ems="10"
安卓:id="@+id/AfternoonText"
安卓:hint="0-6"
安卓:autoText="true"
安卓:background="#ffd1d1d1"
安卓:editable="false"
安卓:textSize="@dimen/abc_text_size_display_1_material"
安卓:layout_alignTop="@+id/MorningText"
安卓:layout_alignLeft="@+id/afternoonTime"
安卓:layout_alignStart="@+id/afternoonTime"
安卓:layout_alignRight="@+id/afternoonTime"
安卓:layout_alignEnd="@+id/afternoonTime"
安卓:text="@string/AfternoonInput"
安卓:textAlignment="center"
安卓:numeric="integer" />
<EditText
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:inputType="number"
安卓:ems="10"
安卓:id="@+id/EveningText"
安卓:hint="0-6"
安卓:autoText="true"
安卓:background="#ffd1d1d1"
安卓:editable="false"
安卓:textSize="@dimen/abc_text_size_display_1_material"
安卓:layout_alignBaseline="@+id/AfternoonText"
安卓:layout_alignBottom="@+id/AfternoonText"
安卓:layout_alignRight="@+id/textView5"
安卓:layout_alignEnd="@+id/textView5"
安卓:text="@string/EveningInput"
安卓:textAlignment="center"
安卓:numeric="integer"
安卓:layout_alignLeft="@+id/textView5"
安卓:layout_alignStart="@+id/textView5" />
<TextView
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:textAppearance="?安卓:attr/textAppearanceSmall"
安卓:text="@string/timeLabel"
安卓:id="@+id/TimeText"
安卓:layout_above="@+id/btnNext"
安卓:layout_alignLeft="@+id/btnNext"
安卓:layout_alignStart="@+id/btnNext" />
<Button
style="?安卓:attr/buttonStyleSmall"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:text="c"
安卓:onClick="calculateClickHandler"
安卓:id="@+id/btnc"
安卓:layout_above="@+id/TimeText"
安卓:layout_toRightOf="@+id/afternoonTime"
安卓:layout_toEndOf="@+id/afternoonTime"
安卓:layout_marginBottom="42dp" />
这是java代码。我想我错了。我怎样才能解决这个问题?任何人帮助:
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;
}
# 1 楼答案
此代码:
绝对是错误的,甚至不应该编译。你在这里说:如果
freetime
小于零。。。做点什么。您必须在其中输入一个值(例如:此外,不需要强制转换此返回值,因为它都是整数:
如果您想让您的代码更好一点(主要是为了可读性),您可以对此进行优化: