我需要一些帮助。我做了一个BMI计算器应用程序。我需要有关如何突出显示ImageButton的帮助。所以我得到了4个ImageButton - imageUnder,imageNo,imageOve和imageOb ..如果BMI结果是OBESE,则imageOb将突出显示,而其他人则不会。(如果体重不足,imageUnder将被突出显示......等等)但是问题是,我不知道该怎么做。 =(
SampleActivity.java:
package com.sample.sample;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.Menu;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.ImageButton;
import android.widget.Toast;
import android.view.View.OnClickListener;
public class SampleActivity extends Activity {
ImageButton imageButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sample);
addListenerOnButton();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.sample, menu);
return true;
}
public void calculateClickHandler(View view) {
if (view.getId() == R.id.calculateButton) {
EditText weightText = (EditText) findViewById(R.id.weightText);
EditText heightText = (EditText)findViewById(R.id.heightText);
TextView resultText = (TextView)findViewById(R.id.resultLabel);
int weight = (int) Float.parseFloat(weightText.getText().toString());
int height = (int) Float.parseFloat(heightText.getText().toString());
int bmiValue = calculateBMI(weight, height);
String bmiInterpretation = interpretBMI(bmiValue);
resultText.setText("Your BMI is:" + " " + bmiValue + " " + "and you are" + " " + bmiInterpretation); }
}
private int calculateBMI (int weight, int height) {
return (int) weight * 703 / (height * height) ;
}
private String interpretBMI(int bmiValue) {
if (bmiValue < 18.5) {
return "Underweight";
} else if (bmiValue < 25){
return "Normal";
} else if (bmiValue < 30) {
return "Overweight";
} else {
return "Obese"; }
}
public void addListenerOnButton() {
imageButton = (ImageButton) findViewById(R.id.imageUnder);
imageButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Toast.makeText(SampleActivity.this,
"Are you Underweight? Eat up!", Toast.LENGTH_SHORT).show();
}
});
imageButton = (ImageButton) findViewById(R.id.imageNormal);
imageButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Toast.makeText(SampleActivity.this,
"Healthy? Nice job!", Toast.LENGTH_SHORT).show();
}
});
imageButton = (ImageButton) findViewById(R.id.imageOver);
imageButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Toast.makeText(SampleActivity.this,
"Are you Overweight? Have an Exercise!", Toast.LENGTH_SHORT).show();
}
});
imageButton = (ImageButton) findViewById(R.id.imageob);
imageButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Toast.makeText(SampleActivity.this,
"Obese? Yucks! Lose some weight!", Toast.LENGTH_SHORT).show();
}
});
}
}
sample_activity.xml:
<RelativeLayout xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".SampleActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/weightLabel"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/weightText"
android:layout_width="70dp"
android:layout_height="50dp"
android:layout_alignRight="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginRight="14dp"
android:layout_marginTop="14dp"
android:ems="10"
android:inputType="number"
android:maxLength="3"
android:text="@string/weightText" >
<requestFocus android:layout_width="wrap_content" />
</EditText>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView1"
android:layout_alignBottom="@+id/textView1"
android:layout_marginLeft="45dp"
android:layout_toRightOf="@+id/textView1"
android:text="@string/heightLabel"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/heightText"
android:layout_width="70dp"
android:layout_height="50dp"
android:layout_alignBaseline="@+id/weightText"
android:layout_alignBottom="@+id/weightText"
android:layout_alignRight="@+id/textView2"
android:ems="10"
android:inputType="number"
android:maxLength="2"
android:text="@string/heightText" />
<TextView
android:id="@+id/gender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/weightText"
android:layout_marginTop="16dp"
android:text="@string/genderLabel"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="@+id/calculateButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/heightText"
android:layout_alignTop="@+id/gender"
android:onClick="calculateClickHandler"
android:text="@string/calculateButton" />
<RadioGroup
android:id="@+id/rdGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/gender"
android:layout_toRightOf="@+id/gender" >
<RadioButton
android:id="@+id/male"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="@string/maleLabel" />
<RadioButton
android:id="@+id/female"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/femaleLabel" />
</RadioGroup>
<TextView
android:id="@+id/resultLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/imageNormal"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/rdGroup"
android:text="@string/emptyString"
android:textColor="#000000"
android:textStyle="bold" />
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/frameLayout"
android:background="#b2ff7c">
</FrameLayout>
<ImageButton
android:id="@+id/imageNormal"
android:layout_width="55dp"
android:layout_height="105dp"
android:layout_alignLeft="@+id/rdGroup"
android:layout_alignTop="@+id/imageUnder"
android:layout_marginLeft="16dp"
android:src="@drawable/no" />
<ImageButton
android:id="@+id/imageOver"
android:layout_width="55dp"
android:layout_height="105dp"
android:layout_alignLeft="@+id/textView2"
android:layout_below="@+id/resultLabel"
android:src="@drawable/ove" />
<ImageButton
android:id="@+id/imageUnder"
android:layout_width="50dp"
android:layout_height="105dp"
android:layout_alignRight="@+id/gender"
android:layout_below="@+id/rdGroup"
android:layout_marginTop="52dp"
android:src="@drawable/und" />
<ImageButton
android:id="@+id/imageob"
android:layout_width="60dp"
android:layout_height="105dp"
android:layout_alignRight="@+id/calculateButton"
android:layout_alignTop="@+id/imageOver"
android:src="@drawable/ob" />
</RelativeLayout>
大家好!因为我在突出显示的东西中感到困惑..所以如果可能的话,我宁愿做这个......可见/不可见的图像按钮..
我有4个imageButton-imageUnder,imageNo,imageOve,imageOB,..如何设置它们不可见???然后使其中一个imageButton可见? ...例如,if(bmiValue&lt; 18.5){return“Underweight”; ,然后imageUnder将可见或显示...
抱歉要求太多..&gt; _&lt;