我有这个imageView,我想首先看不见...... 然后当我单击一个按钮(calculateButton)时,imageView将是可见的。
这是我的ImageView:
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/resultLabel"
android:layout_below="@+id/resultLabel"
android:src="@drawable/image" />
这是我的calculateButton代码:
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 + " " + bmiInterpretation); }
}
private int calculateBMI (int weight, int height) {
return (int) weight * 703 / (height * height) ;
}
private String interpretBMI(int bmiValue) {
if (bmiValue <= 16.0 && bmiValue <= 18.5) {
return "Underweight";
} else if (bmiValue > 18.5 && bmiValue <= 25 ){
return "Normal (Healthy)";
} else if (bmiValue > 25 && bmiValue <= 30 ) {
return "Overweight";
} else {
return "Obese"; }
}
}
答案 0 :(得分:6)
首先使图像视图不可见,然后在onclick内部使其可见。
所以把这一行放在XML的图像视图中
android:visibility="invisible"
所以你的完整代码看起来像这样。
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/resultLabel"
android:layout_below="@+id/resultLabel"
android:visibility="invisible"
android:src="@drawable/image" />
然后在按钮点击事件中显示它。
imageview.setVisibility(View.visible);
请记住,imageview
是ImageView上的对象。所以它看起来像
ImageView imageview =(ImageView) findViewById(R.id.imageView1);
imageview.setVisibility(View.visible);
所以就这样说吧。
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);
ImageView imageview =(ImageView) findViewById(R.id.imageView1);
imageview.setVisibility(View.visible);
.......
.......
if (bmiValue <= 16.0 && bmiValue <= 18.5){
imageview1.setVisibility(View.visible);
imageview2.setVisibility(View.invisible);
imageview3.setVisibility(View.invisible);
imageview4.setVisibility(View.invisible);
}else if (your second condition){
imageview1.setVisibility(View.invisible);
imageview2.setVisibility(View.visible);
imageview3.setVisibility(View.invisible);
imageview4.setVisibility(View.invisible);
}else if (your third condition){
imageview1.setVisibility(View.invisible);
imageview2.setVisibility(View.invisible);
imageview3.setVisibility(View.visible);
imageview4.setVisibility(View.invisible);
}else
{
imageview1.setVisibility(View.invisible);
imageview2.setVisibility(View.invisible);
imageview3.setVisibility(View.invisible);
imageview4.setVisibility(View.visible);
}
这里只需用您的ID替换您的图片视图,然后尝试。希望这会对你有所帮助。
答案 1 :(得分:2)
您可以设置可见性
android:visibility="invisible"
然后点击按钮
public void calculateClickHandler(View view) {
ImageView iv =(ImageView) findViewById(R.id.imageView1);
iv.setVisibility(View.VISIBLE);
}
http://developer.android.com/reference/android/view/View.html#attr_android:visibility
每次按下按钮时都避免初始化imageview
ImageView iv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alarm_manager);
iv =(ImageView) findViewById(R.id.imageView1);
}
答案 2 :(得分:1)
OnCreate()
设置imageview 不可见,例如imageview.setVisibility(View.INVISIBLE);
和按钮onClick()
VISIBLE 就像imageview.setVisibility(View.VISIBLE);
答案 3 :(得分:0)
尝试一下
imageview的可见性是看不见的......
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/resultLabel"
android:layout_below="@+id/resultLabel"
android:src="@drawable/image"
android:visibility="invisible" />
并在您的代码中...,在按钮上点击此行
imageview.setVisibility(View.visible);
答案 4 :(得分:0)
您可能应该在布局XML中将图像定义为不可见... android:visibility="invisible"
您可以使用view.setVisibility(x);
修改视图的展示率,其中x为View.INVISIBLE
,View.VISIBLE
或View.GONE
。
用onCreate()
方法编写此代码。
Button calculateButton = (Button) findViewById(R.id.button1);
calculateButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
ImageView imageView =(ImageView) findViewById(R.id.imageView1);
imageView.setVisibility(View.VISIBLE);
}
});
答案 5 :(得分:0)
首先在xml中设置可见性
android:visibility="gone"
现在可以在onclick上看到java代码集可见性..
imageView.setVisibility(View.VISIBLE);