为啥显示的是同一张图片
来源:8-4 作业题
慕桂英5369657
2018-11-25 13:04:46
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:text="无线局域网" />
<ToggleButton
android:id="@+id/wifi"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:textOff="关闭wifi"
android:textOn="打开wifi"/>
<RadioGroup
android:id="@+id/se_wifi"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone">
<RadioButton
android:id="@+id/office"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="imooc-office"/>
<RadioButton
android:id="@+id/meeting"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="imooc-meeting"/>
<RadioButton
android:id="@+id/visitor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="imooc-visitor"/>
<ImageView
android:id="@+id/imageoffice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:src="@drawable/office"
/>
<ImageView
android:id="@+id/imagemeeting"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:src="@drawable/meeting"
/>
<ImageView
android:id="@+id/imagevisitor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:src="@drawable/visitor"
/>
</RadioGroup>
</LinearLayout>
package com.imooc.wifiproj;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.ToggleButton;
public class MainActivity extends Activity {
private ToggleButton toggleButton;
private RadioGroup rg;
private RadioButton rb_offic,rb_meeting,rb_visitor;
private ImageView imageoffice,imagemeeting,imagevisitor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_linearlayout);
toggleButton=(ToggleButton) findViewById(R.id.wifi);
rg=(RadioGroup) findViewById(R.id.se_wifi);
rb_offic=(RadioButton) findViewById(R.id.office);
rb_meeting=(RadioButton) findViewById(R.id.meeting);
rb_visitor=(RadioButton) findViewById(R.id.visitor);
imageoffice=(ImageView) findViewById(R.id.imageoffice);
imagemeeting=(ImageView) findViewById(R.id.imagemeeting);
imagevisitor=(ImageView) findViewById(R.id.imagevisitor);
rg.setOnCheckedChangeListener(new MyRadioButtonListener());
toggleButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(toggleButton.isChecked()){
//设置rg可见属性
rg.setVisibility(v.VISIBLE);
}else{
//设置rg不可见属性
rg.setVisibility(v.GONE);
}
}
});
}
class MyRadioButtonListener implements OnCheckedChangeListener{
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
switch(checkedId){
case R.id.office:
imageoffice.setImageResource(R.drawable.office);
imageoffice.setVisibility(imageoffice.VISIBLE);
break;
case R.id.meeting:
imagemeeting.setImageResource(R.drawable.meeting);
imagemeeting.setVisibility(imagemeeting.VISIBLE);
break;
case R.id.visitor:
imagevisitor.setImageResource(R.drawable.visitor);
imagevisitor.setVisibility(imagevisitor.VISIBLE);
break;
}
}
}
}
1回答
irista23
2018-11-26
因为你图片设置的选择后都依稀显示,没有隐藏其他的,所以布局上都是显示第一次选择的,可以参照下图中的代码做如下修改。
相似问题