我有这一系列的复选框..我想要什么时候检查剩下的应该消失一直试图这样做它一直给我一些头痛...最近..如果你能帮助我。
XML
<?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:id="@+id/tvShow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="110.29"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ScrollView
android:layout_width="fill_parent"
android:layout_height="390dp"
android:layout_weight="110.29" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="vertical" >
<CheckBox
android:id="@+id/cbEng"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ENGLISH" />
<CheckBox
android:id="@+id/cbMat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MATHEMATICS" />
<CheckBox
android:id="@+id/cbPhy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PHYSICS" />
<CheckBox
android:id="@+id/cbChe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CHEMISTRY" />
<CheckBox
android:id="@+id/cbBio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BIOLOGY" />
<CheckBox
android:id="@+id/cbGeo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="GEOGRAPHY" />
<CheckBox
android:id="@+id/cbAcc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="FINANCIAL ACCOUNT" />
<CheckBox
android:id="@+id/cbGov"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="GOVERNMENT" />
<CheckBox
android:id="@+id/cbEco"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ECONOMICS" />
<CheckBox
android:id="@+id/cbCrk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CRK" />
<CheckBox
android:id="@+id/cbLit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="LITERARURE" />
</LinearLayout>
</ScrollView>
</LinearLayout>
然后在Java
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;
public class SubComb extends Activity {
TextView tvShow;
CheckBox Eng, Mat, Phy, Che, Bio, Geo, Acc, Gov, Eco, Crk, Lit;
int count = 0;
String[] Slot = { "1", "2", "3", "4" };
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.subcomb);
xmlConnect();
Eng.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
Slot[count] = "Eng";
String txCount = Integer.toString(count);
tvShow.setText(txCount);
count++;
if (count == 3){Eng.setVisibility(View.GONE);
Mat.setVisibility(View.GONE);
Phy.setVisibility(View.GONE);
Che.setVisibility(View.GONE);
Bio.setVisibility(View.GONE);
Geo.setVisibility(View.GONE);
Acc.setVisibility(View.GONE);
Gov.setVisibility(View.GONE);
Eco.setVisibility(View.GONE);
Crk.setVisibility(View.GONE);
Lit.setVisibility(View.GONE);
}
Toast.makeText(getApplicationContext(), "Checked",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Not Checked",
Toast.LENGTH_SHORT).show();
count--;
Slot[count] = null;
String txCount = Integer.toString(count);
tvShow.setText(txCount);
}
}
});
Mat.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
Slot[count] = "Mat";
String txCount = Integer.toString(count);
tvShow.setText(txCount);
count++;
Toast.makeText(getApplicationContext(), "Checked",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Not Checked",
Toast.LENGTH_SHORT).show();
count--;
Slot[count] = null;
String txCount = Integer.toString(count);
tvShow.setText(txCount);
}
}
});
Phy.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
Slot[count] = "Phy";
String txCount = Integer.toString(count);
tvShow.setText(txCount);
count++;
Toast.makeText(getApplicationContext(), "Checked",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Not Checked",
Toast.LENGTH_SHORT).show();
count--;
Slot[count] = null;
String txCount = Integer.toString(count);
tvShow.setText(txCount);
}
}
});
Che.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
Slot[count] = "Che";
String txCount = Integer.toString(count);
tvShow.setText(txCount);
count++;
Toast.makeText(getApplicationContext(), "Checked",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Not Checked",
Toast.LENGTH_SHORT).show();
count--;
Slot[count] = null;
String txCount = Integer.toString(count);
tvShow.setText(txCount);
}
}
});
Bio.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
Slot[count] = "Bio";
String txCount = Integer.toString(count);
tvShow.setText(txCount);
count++;
Toast.makeText(getApplicationContext(), "Checked",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Not Checked",
Toast.LENGTH_SHORT).show();
count--;
Slot[count] = null;
String txCount = Integer.toString(count);
tvShow.setText(txCount);
}
}
});
Geo.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
Slot[count] = "Geo";
String txCount = Integer.toString(count);
tvShow.setText(txCount);
count++;
Toast.makeText(getApplicationContext(), "Checked",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Not Checked",
Toast.LENGTH_SHORT).show();
count--;
Slot[count] = null;
String txCount = Integer.toString(count);
tvShow.setText(txCount);
}
}
});
Acc.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
Slot[count] = "Acc";
String txCount = Integer.toString(count);
tvShow.setText(txCount);
count++;
Toast.makeText(getApplicationContext(), "Checked",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Not Checked",
Toast.LENGTH_SHORT).show();
count--;
Slot[count] = null;
String txCount = Integer.toString(count);
tvShow.setText(txCount);
}
}
});
Gov.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
Slot[count] = "Gov";
String txCount = Integer.toString(count);
tvShow.setText(txCount);
count++;
Toast.makeText(getApplicationContext(), "Checked",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Not Checked",
Toast.LENGTH_SHORT).show();
count--;
Slot[count] = null;
String txCount = Integer.toString(count);
tvShow.setText(txCount);
}
}
});
Eco.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
Slot[count] = "Eco";
String txCount = Integer.toString(count);
tvShow.setText(txCount);
count++;
Toast.makeText(getApplicationContext(), "Checked",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Not Checked",
Toast.LENGTH_SHORT).show();
count--;
Slot[count] = null;
String txCount = Integer.toString(count);
tvShow.setText(txCount);
}
}
});
Crk.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
Slot[count] = "Crk";
String txCount = Integer.toString(count);
tvShow.setText(txCount);
count++;
Toast.makeText(getApplicationContext(), "Checked",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Not Checked",
Toast.LENGTH_SHORT).show();
count--;
Slot[count] = null;
String txCount = Integer.toString(count);
tvShow.setText(txCount);
}
}
});
Lit.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
Slot[count] = "Lit";
String txCount = Integer.toString(count);
tvShow.setText(txCount);
count++;
Toast.makeText(getApplicationContext(), "Checked",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Not Checked",
Toast.LENGTH_SHORT).show();
count--;
Slot[count] = null;
String txCount = Integer.toString(count);
tvShow.setText(txCount);
}
}
});
}
private void xmlConnect() {
tvShow = (TextView) findViewById(R.id.tvShow);
Eng = (CheckBox) findViewById(R.id.cbEng);
Mat = (CheckBox) findViewById(R.id.cbMat);
Phy = (CheckBox) findViewById(R.id.cbPhy);
Che = (CheckBox) findViewById(R.id.cbChe);
Bio = (CheckBox) findViewById(R.id.cbBio);
Geo = (CheckBox) findViewById(R.id.cbGeo);
Acc = (CheckBox) findViewById(R.id.cbAcc);
Gov = (CheckBox) findViewById(R.id.cbGov);
Eco = (CheckBox) findViewById(R.id.cbEco);
Crk = (CheckBox) findViewById(R.id.cbCrk);
Lit = (CheckBox) findViewById(R.id.cbLit);
}
}
答案 0 :(得分:0)
为什么不在Activity中实现CheckBox.OnCheckedChangeListener,而不是设置不同的CheckChangedListener,您可以将其作为
public class SubComb implements CheckBox.OnCheckedChangeListener {
int count=0;
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if(b){
count++;
if(count==4){
if(!Mat.isChecked()) Mat.setVisibility(View.GONE);
if(!Phy.isChecked()) Phy.setVisibility(View.GONE);
if(!Che.isChecked()) Che.setVisibility(View.GONE);
if(!Bio.isChecked()) Bio.setVisibility(View.GONE);
if(!Geo.isChecked()) Geo.setVisibility(View.GONE);
if(!Acc.isChecked()) Acc.setVisibility(View.GONE);
if(!Gov.isChecked()) Gov.setVisibility(View.GONE);
if(!Eco.isChecked()) Eco.setVisibility(View.GONE);
if(!Crk.isChecked()) Crk.setVisibility(View.GONE);
if(!Lit.isChecked()) Lit.setVisibility(View.GONE);
}
}
else{
count--;
Mat.setVisibility(View.VISIBLE);
Phy.setVisibility(View.VISIBLE);
Che.setVisibility(View.VISIBLE);
Bio.setVisibility(View.VISIBLE);
Geo.setVisibility(View.VISIBLE);
Acc.setVisibility(View.VISIBLE);
Gov.setVisibility(View.VISIBLE);
Eco.setVisibility(View.VISIBLE);
Crk.setVisibility(View.VISIBLE);
Lit.setVisibility(View.VISIBLE);
}
}
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.subcomb);
Eng = (CheckBox) findViewById(R.id.cbEng);
Mat = (CheckBox) findViewById(R.id.cbMat);
Phy = (CheckBox) findViewById(R.id.cbPhy);
Che = (CheckBox) findViewById(R.id.cbChe);
Bio = (CheckBox) findViewById(R.id.cbBio);
Geo = (CheckBox) findViewById(R.id.cbGeo);
Acc = (CheckBox) findViewById(R.id.cbAcc);
Gov = (CheckBox) findViewById(R.id.cbGov);
Eco = (CheckBox) findViewById(R.id.cbEco);
Crk = (CheckBox) findViewById(R.id.cbCrk);
Lit = (CheckBox) findViewById(R.id.cbLit);
Eng.setOnCheckedChangeListener(this);
Mat.setOnCheckedChangeListener(this);
Phy.setOnCheckedChangeListener(this);
Che.setOnCheckedChangeListener(this);
Bio.setOnCheckedChangeListener(this);
Geo.setOnCheckedChangeListener(this);
Acc.setOnCheckedChangeListener(this);
Gov.setOnCheckedChangeListener(this);
Eco.setOnCheckedChangeListener(this);
Crk.setOnCheckedChangeListener(this);
Lit.setOnCheckedChangeListener(this);
}
}
}